[英]Core Data App group synchronization (w/ extension)
The widgets in our iOS App are custom, and therefore I added a feature to remove parts of the widget. 我们的iOS应用中的小部件是自定义的,因此我添加了一项功能以删除小部件的一部分。 To save settings for the widgets etc. our widgets share Core Data via App groups.
为了保存小部件等的设置,我们的小部件通过App组共享核心数据。 However when I delete something from the widget it doesnt seem to always sync correctly.
但是,当我从小部件中删除某些内容时,它似乎始终无法正确同步。 This happens primarily when the app is active in memory.
这主要在应用程序在内存中处于活动状态时发生。
When I delete something I call this: 当我删除某些内容时,我称之为:
-(void)removeWidgetFromUser:(UserModel *)user Widget:(Widget *)widget{
if(widget != nil){
[widgetContext deleteObject:widget];
NSError *error;
if (![widgetContext save:&error]) {
NSLog(@"Unable to remove widget %@", error);
}
}
}
Then I use wormhole to sync the core data in my app and it calls this: 然后,我使用wormhole来同步我的应用程序中的核心数据,它将此称为:
-(void)updateCoreData{
[self.managedObjectContext refreshAllObjects];
}
I am sure both the methods get called. 我确信这两种方法都会被调用。 But sometimes the app sees a widget I just removed, and then it also happens to reappear in my Widget.
但是有时应用程序会看到我刚刚删除的小部件,然后它也恰好重新出现在我的小部件中。
EDIT: I think whats happening is that the CoreData context in my app doesnt update correctly and then the widget actually syncs with the CoreData in my app. 编辑:我认为正在发生的事情是我的应用程序中的CoreData上下文未正确更新,然后该小部件实际上与我的应用程序中的CoreData同步。 Therefore the deleted widget re-appears after some time.
因此,删除的窗口小部件会在一段时间后重新出现。 Still figuring it out...
仍在弄清楚...
I finally did it. 我终于做到了。 By implementing the following code:
通过实现以下代码:
- (id)initWithCoder:(NSCoder *)decoder {
NSManagedObjectContext *context = [SharedCoreDataObjects sharedInstance].managedObjectContext; // use your NSManagedObjectContext
NSPersistentStoreCoordinator *coordinator = [SharedCoreDataObjects sharedInstance].persistentStoreCoordinator; //use your NSPersistentStoreCoordinator
NSURL *url = (NSURL *)[decoder decodeObjectForKey:@"URIRepresentation"];
NSManagedObjectID *managedObjectID = [coordinator managedObjectIDForURIRepresentation:url];
self = [context existingObjectWithID:managedObjectID error:nil];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:[[self objectID] URIRepresentation] forKey:@"URIRepresentation"];
}
in my NSManagedObjects I was able use MMWormhole to send the NSManagedObjectContextDidSaveNotification
to the App and then call 在我的NSManagedObjects中,我可以使用MMWormhole将
NSManagedObjectContextDidSaveNotification
发送到应用程序,然后调用
[context mergeChangesFromContextDidSaveNotification:messageObject];
To let the context merge the changes. 让上下文合并更改。 This seems to work perfectly for now!
目前看来,这很完美!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.