背景
我已经发布了一个关于基础知识的问题在进程之间共享核心数据存储.
我正在尝试实施给出的建议,但遇到了问题。
My Goal
我有两个进程 - Helper App 和 UI。它们都共享一个数据存储。当助手应用程序将新数据保存到商店时,我希望 UI 更新它的 NSManagedObjectContext 。
当前程序流程
Helper App Process 将数据写入 Store。
在 Helper 应用程序中,我监听 NSManagedObjectContextDidSaveNotification 通知。
保存上下文时,我使用 URI 表示和 NSArchiver 对插入、删除和更新的对象进行编码。
我使用此编码字典作为 userInfo 向 NSDistributedNotificationCenter 发送 NSNotification。
UI 进程正在侦听保存通知。当它收到通知时,它使用 NSUnarchiver 取消归档 userInfo。
它从给定的 URI 中查找所有更新/插入/删除的对象,并将它们替换为 NSManagedObjects。
它使用更新/插入/删除的对象构造一个 NSNotification。
我在 UI Process 的托管对象上下文上调用 mergeChangesFromContextDidSaveNotification: ,传入我在上一步中构造的 NSNotification。
问题
插入的对象会正确地进入 UI 托管对象上下文并出现在 UI 中。问题来自更新的对象。他们只是不更新。
我尝试过的
最明显要尝试的事情是
是通过保存通知
从 Helper App 进程到
用户界面流程。容易,对吧?嗯,不。
分布式通知不会
让我以 userInfo 的身份执行此操作
字典不在右边
格式。这就是为什么我要做所有的事情
NS存档的东西。
我试过打电话
刷新对象:合并更改:是
要更新的 NSManagedObjects,
但这似乎没有任何
影响。
我尝试过执行
mergeChangesFromContextDidSaveNotification:
主线程上的选择器和
当前线程。似乎两者都没有
影响结果。
我尝试过使用
mergeChangesFromContextDidSaveNotification:
在线程之间之前,哪个
课程简单得多并且有效
完美。但我也需要这个
进程之间的功能。
备择方案?
我在这里错过了什么吗?我一直有一种感觉,我让这个事情变得比它需要的要复杂得多,但是在阅读了几次文档并花了几天时间研究这个之后,我看不到任何其他方式来刷新 MOC用户界面。
有没有更优雅的方法来做到这一点?或者我只是在代码中的某个地方犯了一个愚蠢的错误?
The Code
我已尝试使其尽可能具有可读性,但仍然很混乱。对不起。
助手应用程序代码
-(void)workerThreadObjectContextDidSave:(NSNotification *)saveNotification {
NSMutableDictionary *savedObjectsEncodedURIs = [NSMutableDictionary dictionary];
NSArray *savedObjectKeys = [[saveNotification userInfo] allKeys];
for(NSString *thisSavedObjectKey in savedObjectKeys) {
// This is the set of updated/inserted/deleted NSManagedObjects.
NSSet *thisSavedObjectSet = [[saveNotification userInfo] objectForKey:thisSavedObjectKey];
NSMutableSet *thisSavedObjectSetEncoded = [NSMutableSet set];
for(id thisSavedObject in [thisSavedObjectSet allObjects]) {
// Construct a set of URIs that will be encoded as NSData
NSURL *thisSavedObjectURI = [[(NSManagedObject *)thisSavedObject objectID] URIRepresentation];
[thisSavedObjectSetEncoded addObject:thisSavedObjectURI];
}
// Archive the set of URIs.
[savedObjectsEncodedURIs setObject:[NSArchiver archivedDataWithRootObject:thisSavedObjectSetEncoded] forKey:thisSavedObjectKey];
}
if ([[savedObjectsEncodedURIs allValues] count] > 0) {
// Tell UI process there are new objects that need merging into it's MOC
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.synapticmishap.lapsus.save" object:@"HelperApp" userInfo:(NSDictionary *)savedObjectsEncodedURIs];
}
}
UI Code
-(void)mergeSavesIntoMOC:(NSNotification *)notification {
NSDictionary *objectsToRefresh = [notification userInfo];
NSMutableDictionary *notificationUserInfo = [NSMutableDictionary dictionary];
NSArray *savedObjectKeys = [[notification userInfo] allKeys];
for(NSString *thisSavedObjectKey in savedObjectKeys) {
// Iterate through all the URIs in the decoded set. For each URI, get the NSManagedObject and add it to a set.
NSSet *thisSavedObjectSetDecoded = [NSUnarchiver unarchiveObjectWithData:[[notification userInfo] objectForKey:thisSavedObjectKey]];
NSMutableSet *savedManagedObjectSet = [NSMutableSet set];
for(NSURL *thisSavedObjectURI in thisSavedObjectSetDecoded) {
NSManagedObject *thisSavedManagedObject = [managedObjectContext objectWithID:[persistentStoreCoordinator managedObjectIDForURIRepresentation:thisSavedObjectURI]];
[savedManagedObjectSet addObject:thisSavedManagedObject];
// If the object is to be updated, refresh the object and merge in changes.
// This doesn't work!
if ([thisSavedObjectKey isEqualToString:NSUpdatedObjectsKey]) {
[managedObjectContext refreshObject:thisSavedManagedObject mergeChanges:YES];
[managedObjectContext save:nil];
}
}
[notificationUserInfo setObject:savedManagedObjectSet forKey:thisSavedObjectKey];
}
// Build a notification suitable for merging changes into MOC.
NSNotification *saveNotification = [NSNotification notificationWithName:@"" object:nil userInfo:(NSDictionary *)notificationUserInfo];
[managedObjectContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
withObject:saveNotification
waitUntilDone:YES];
}
我使用了中的方法
http://www.mlsite.net/blog/?p=518
然后每个对象都正确地出现故障,但故障是在缓存中获取的,因此仍然没有更新
我必须做
[moc stalenessInterval = 0];
最终,通过关系,它奏效了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)