gpt4 book ai didi

ios - 解析查询在重启时返回 PFObject 而不是子类

转载 作者:行者123 更新时间:2023-11-28 05:27:30 25 4
gpt4 key购买 nike

我有以下 PFObject 子类:

class Show: PFObject, PFSubclassing {

@NSManaged var date: NSDate

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

static func parseClassName() -> String {
return "Show"
}
}

然后我可以添加新的 Show 对象,如下所示:

let newShow = Show()
newShow.date = NSDate()
newShow.pinInBackground()

并查询Show对象。

let query = Show.query()!
query.fromLocalDatastore()
query.orderByAscending("date")
query.findObjectsInBackgroundWithBlock { (shows, error) -> Void in

println("shows:\(shows)")

let myShows = shows as? [Show]

println("myShows:\(myShows)")

let myObjects = shows as? [PFObject]

println("myObjects:\(myObjects)")
}

按预期返回 [Show]:

shows:Optional([<Show: 0x7fde6a8ba0f0, objectId: new, localId: local_90293157b4a20c5c> {
date = "2015-06-01 12:08:16 +0000";
}])

myShows:Optional([<Show: 0x7fde6a8ba0f0, objectId: new, localId: local_90293157b4a20c5c> {
date = "2015-06-01 12:08:16 +0000";
}])

myObjects:Optional([<Show: 0x7fde6a8ba0f0, objectId: new, localId: local_90293157b4a20c5c> {
date = "2015-06-01 12:08:16 +0000";
}])

但是,如果我终止应用,然后重新运行相同的查询,结果数组是[PFObject] 而不是[Show]

shows:Optional([<Show: 0x7fef632ca2b0, objectId: new, localId: local_c6995edd73e73e10> {
date = "2015-06-01 12:08:16 +0000";
}])

myShows:nil

myObjects:Optional([<Show: 0x7fef632ca2b0, objectId: new, localId: local_c6995edd73e73e10> {
date = "2015-06-01 12:08:16 +0000";
}])

我错过了什么吗?为什么在重新启动应用程序后没有返回正确类的对象?

最佳答案

好的,所以我在 Root View Controller 的 viewDidLoad 方法中运行查询。检查 registerSubclassPFSubclassing 文档:

@warning This method must be called before <[Parse setApplicationId:clientKey:]>

根据 Parse 文档中的示例,我重写了 initialize 方法,但是在创建查询时会调用此方法,但会在调用 appDidFinishLaunching 方法之前调用 setApplicationId:clientKey

所以关键是稍后运行查询,或者更早地注册子类!

关于ios - 解析查询在重启时返回 PFObject 而不是子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30573382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com