ios - 为什么在注册子类时出现错误 "the class ... must be registered with registerSubclass before using Parse"?

标签 ios swift parse-platform ios8 pfsubclassing

从 iOS parse-library-1.6.3 升级到 parse-library 1.7.5 后,我在我的一个 PFQueryTableViewController 的 queryForTable 中收到以下错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The class Appname.AttendingModel must be registered with registerSubclass before using Parse.'

但是,我在 application:didFinishLaunchingWithOptions: 中注册子类,使用以下代码:

    AttendingModel.initialize()
    Parse.enableLocalDatastore()
    Parse.setApplicationId(Config.parseAppId, clientKey: Config.parseClientKey)

AttentingModel 的初始化方法如下所示:

override class func initialize() {
    var onceToken : dispatch_once_t = 0;
    dispatch_once(&onceToken) {
        self.registerSubclass()
    }
}

并且还设置了解析类名:

class func parseClassName() -> String {
    return "Attending"
}

我已验证我的 initialize 方法中的 self.registerSubclass() 行在应用程序启动时被调用,但我仍然遇到错误。

我应该补充一点,我正在使用 Xcode 6.4 并在 iOS 8.4 上运行。

这是错误的完整堆栈跟踪:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The class Appname.AttendingModel must be registered with registerSubclass before using Parse.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000108354c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000107fedbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000108354b9d +[NSException raise:format:] + 205
    3   Appname                             0x00000001060cb9f0 +[PFObject query] + 120
    4   Appname                             0x000000010607fe73 _TFC8Appname 12UsersTableVC13queryForTablefS0_FT_CSo7PFQuery + 131
    5   Appname                             0x00000001060800c2 _TToFC8Appname 12UsersTableVC13queryForTablefS0_FT_CSo7PFQuery + 34
    6   Appname                             0x000000010613fac5 -[PFQueryTableViewController loadObjects:clear:] + 106
    7   Appname                             0x000000010613f3b7 -[PFQueryTableViewController viewDidLoad] + 59
    8   Appname                             0x000000010607fb3d _TFC8Appname 12UsersTableVC11viewDidLoadfS0_FT_T_ + 61
    9   Appname                             0x000000010607fde2 _TToFC8Appname 12UsersTableVC11viewDidLoadfS0_FT_T_ + 34
    10  UIKit                               0x0000000108c211d0 -[UIViewController loadViewIfRequired] + 738
    11  UIKit                               0x0000000108c213ce -[UIViewController view] + 27
    12  Appname                             0x0000000106097e13 _TFC8Appname 7UsersVC11viewDidLoadfS0_FT_T_ + 1347
    13  Appname                             0x0000000106098392 _TToFC8Appname 7UsersVC11viewDidLoadfS0_FT_T_ + 34
    14  UIKit                               0x0000000108c211d0 -[UIViewController loadViewIfRequired] + 738
    15  UIKit                               0x0000000108c213ce -[UIViewController view] + 27
    16  UIKit                               0x0000000108c46257 -[UINavigationController _startCustomTransition:] + 633
    17  UIKit                               0x0000000108c5237f -[UINavigationController _startDeferredTransitionIfNeeded:] + 386
    18  UIKit                               0x0000000108c52ece -[UINavigationController __viewWillLayoutSubviews] + 43
    19  UIKit                               0x0000000108d9d6d5 -[UILayoutContainerView layoutSubviews] + 202
    20  UIKit                               0x0000000108b709eb -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
    21  QuartzCore                          0x0000000106dceed2 -[CALayer layoutSublayers] + 146
    22  QuartzCore                          0x0000000106dc36e6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    23  QuartzCore                          0x0000000106dc3556 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    24  QuartzCore                          0x0000000106d2f86e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
    25  QuartzCore                          0x0000000106d30a22 _ZN2CA11Transaction6commitEv + 462
    26  QuartzCore                          0x0000000106d310d3 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
    27  CoreFoundation                      0x0000000108287ca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    28  CoreFoundation                      0x0000000108287c00 __CFRunLoopDoObservers + 368
    29  CoreFoundation                      0x000000010827da33 __CFRunLoopRun + 1123
    30  CoreFoundation                      0x000000010827d366 CFRunLoopRunSpecific + 470
    31  GraphicsServices                    0x000000010a83ea3e GSEventRunModal + 161
    32  UIKit                               0x0000000108af08c0 UIApplicationMain + 1282
    33  Appname                             0x0000000105ffd9f7 main + 135
    34  libdyld.dylib                       0x000000010ba8c145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

知道是什么原因造成的吗?我正在使用相同的机制为这个应用程序中的其他几个类注册子类,其他类的 PFQueryTableViewControllers 都工作正常。

最佳答案

问题出在我的 AttendingModel 类的初始化函数中。在函数中使用 dispatch_once_t 作为局部变量并不能确保子类只注册一次,而且子类的不断重新注册显然会导致 Parse 出现问题。

将初始化函数改成这样解决了问题:

private static var onceToken : dispatch_once_t = 0

override class func initialize() {
    dispatch_once(&onceToken) {
        self.registerSubclass()
    }
}

关于ios - 为什么在注册子类时出现错误 "the class ... must be registered with registerSubclass before using Parse"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31798660/

相关文章:

ios - swift:下载百分比

ios - 如何正确设置最短日期并获取选择器 View 上显示的最短日期?

arrays - 无法在 Swift 中编译 array.writeToFile;但在 ObjC 中没关系

swift - 难以检索解析对象字段值

dropbox - Dropbox Datastore API 与 Parse 有何不同?

ios - 导航栏在隐藏时没有动画,但在重新出现时仍然有动画

ios - 魔法记录,无法保存对象 : contextDidSave == NO, error = nil

swift - 在 SpriteKit 中安排音频 - Swift

ios - 在大类上解析 countObjectsInBackground

iOS Swift - 带 slider 的移动图像