我有一个名为Attendee
的类,它继承自PFObject
。在我的applicationDidFinishLaunching()
方法中,我像这样注册子类:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Attendee.initialize()
Parse.enableLocalDatastore()
Parse.setApplicationId(ObjectManager.appID, clientKey: ObjectManager.clientKey)
getData()
return true
}
func getData() {
//create the query
var attendeeQuery = Attendee.query()
attendeeQuery?.fromLocalDatastore()
.fromPinWithName(CacheKeys.AttendeesKey)
.whereKey("conference", equalTo: ObjectManager.currentConf)
//register a background task
var bgTask = UIBackgroundTaskInvalid
bgTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({ () -> Void in
UIApplication.sharedApplication().endBackgroundTask(bgTask)
bgTask = UIBackgroundTaskInvalid
println("Attendees Loader Expiration Handler called")
})
//run the query
attendeeQuery?.findObjectsInBackgroundWithBlock({ (cachedAttendees: [AnyObject]?, error:NSError?) -> Void in
if error != nil {
println(error)
return
}
...
UIApplication.sharedApplication().endBackgroundTask(bgTask)
})
})
}
但是,当我运行代码时,我在行var attendeeQuery = Attendee.query()
处得到一个例外。例外情况说我需要在使用它之前注册Attendee
子类。我不明白为什么会这么说,因为我事先注册了。以下是Attendee
类定义。
class Attendee: PFObject, PFSubclassing {
var name:String
{
return self["name"] as! String
}
...
override class func initialize() {
var onceToken : dispatch_once_t = 0;
dispatch_once(&onceToken) {
self.registerSubclass()
}
}
class func parseClassName() -> String {
return Classes.Attendee
}
}
例外:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The class MedConf.Attendee must be registered with registerSubclass before using Parse.'
*** First throw call stack:
(0x1835002d8 0x194d240e4 0x183500218 0x1001e21d0 0x1001e1fac 0x100135d88 0x100138af0 0x1000e43e0 0x1000e4580 0x187fb3190 0x1881ca854 0x1881cd208 0x1881cb778 0x18bd093c8 0x1834b827c 0x1834b7384 0x1834b59a8 0x1833e12d4 0x187fac43c 0x187fa6fac 0x1000e7b48 0x1953a2a08)
答案 0 :(得分:1)
当我从1.6.3升级到Parse的iOS SDK 1.7.5时,我遇到了同样的问题。问题出在我的初始化函数中,看起来和你的一样。
将dispatch_once_t用作函数中的局部变量不能用于确保子类只注册一次;我注意到在initialize函数中的registerSubclass()调用上放了一个断点,它被多次调用。不断重新注册子类显然会导致新版Parse SDK出现问题。
将初始化函数更改为这样可以解决问题:
private static var onceToken : dispatch_once_t = 0
override class func initialize() {
dispatch_once(&onceToken) {
self.registerSubclass()
}
}
答案 1 :(得分:0)
如何跟踪must be registered with registerSubclass
因此,您需要调用registerSubclass
方法而不是initialize
所以这里是代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Attendee.registerSubclass()
Parse.enableLocalDatastore()
Parse.setApplicationId(ObjectManager.appID, clientKey: ObjectManager.clientKey)
getData()
return true
}