简体   繁体   English

核心数据应用组同步(带扩展)

[英]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.

相关问题 iOS 11+ 如何将现有核心数据迁移到共享应用组以用于扩展? - iOS 11+ How to migrate existing Core Data to Shared App Group for use in extension? 应用程序设计:处理带有背景上下文的核心数据; 合并由 MOC 过滤的通知 - App Design: Handling Core Data w/ Background Contexts ; Merging w/ Notifications Filtered by MOC 在iOS8中在App和Extension之间共享核心数据堆栈和数据 - Sharing a Core Data stack and data between App and Extension in iOS8 在Swift中的应用和今日扩展之间共享核心数据数据库 - Share Core Data database between app and today extension in Swift 应用和扩展程序-使用核心数据==错误:sharedApplication()'不可用 - App and Extension - Use Core data == error : sharedApplication()' is unavailable 从容器应用程序和扩展程序访问核心数据 - Accessing Core Data from both container app and extension iOS - 通知今天扩展主应用程序中的核心数据更改 - iOS - Notify today extension for Core Data changes in the main app 服务扩展写入核心数据,但在应用程序中找不到任何内容 - Service extension writing to core data but nothing found in app WatchKit Extension看不到带有App Group的NSUserDefaults中保存的数据 - WatchKit Extension is not seeing data saved in NSUserDefaults with App Group 在iOS 8扩展程序中访问核心数据SQL数据库(在App和Widget扩展程序之间共享数据) - Accessing Core Data SQL Database in iOS 8 Extension (Sharing Data Between App and Widget Extension)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM