简体   繁体   中英

Why am I getting the error “the class … must be registered with registerSubclass before using Parse” when I am registering the subclass?

After upgrading from iOS parse-library-1.6.3 to parse-library 1.7.5, I'm getting the following error in the queryForTable of one of my PFQueryTableViewControllers:

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

However, I am registering the subclass in application:didFinishLaunchingWithOptions:, using this code:

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

The initialize method of AttendingModel looks like this:

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

and the parse class name is also set:

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

I've verified that the self.registerSubclass() line in my initialize method is being called at application launch, but I'm still getting the error.

I should add that I'm using Xcode 6.4 and running on iOS 8.4.

Here's the full stack trace of the error:

*** 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

Any idea what could be causing this? I'm using the same mechanism to registerSubclass for several other classes in this app, and the PFQueryTableViewControllers for the other classes are all working fine.

The problem was in my initialize function on my AttendingModel class. Using a dispatch_once_t as a local var in a function doesn't serve the purpose of ensuring that the subclass is registered exactly once, and the constant re-registering of the subclass was apparently causing problems for Parse.

Changing the initialize function to look like this fixed the problem:

private static var onceToken : dispatch_once_t = 0

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM