【发布时间】:2016-01-02 11:12:32
【问题描述】:
我在一个线程中将对象保存在数组中。有时我需要从另一个线程访问它。我怎样才能做到这一点?
// Main thread
DownloadVideoObject *obj = [DownloadVideoObject createWithKey:@"13"];
obj.videoURLStr = @"..";
[[RLMRealm defaultRealm] addObject:obj];
[self.downloadObjects addObject:obj];
// in another thread
for (DownloadVideoObject *obj in self.downloadObjects) {
if ([key isEqualToString:obj.key]) { // crash on this line "Realm accessed from incorrect thread"
// ...
}
}
在CoreData 中,我可以通过objectId 在另一个线程中获取对象。我怎样才能在 Realm 中做这样的事情?
编辑
我知道我可以通过主键访问对象,但在另一个线程中我无法访问它。只保存键而不是对象是正确的解决方案吗?
【问题讨论】:
标签: objective-c realm