[英]Exception while registering subclass with Parse
我有一个名为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)
从1.6.3升级到Parse的iOS SDK 1.7.5时,我遇到了同样的问题。 问题出在我的初始化函数中,该函数看起来与您的相同。
在函数中使用dispatch_once_t作为局部变量并不能确保子类只被注册一次。 通过在初始化函数的registerSubclass()调用上放置一个断点,我注意到它被多次调用。 子类的不断重新注册显然导致新版本的Parse SDK出现问题。
更改初始化函数看起来像这样可以解决问题:
private static var onceToken : dispatch_once_t = 0
override class func initialize() {
dispatch_once(&onceToken) {
self.registerSubclass()
}
}
遵循该错误提示该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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.