NSOperation 内的 ASINetworkQueue 保存核心数据时出错

发布于 2024-12-21 16:44:45 字数 214 浏览 2 评论 0原文

我想在 NSOperation 中使用 ASINetworkQueue。这效果很好并且没有任何问题。失败的是保存核心数据。我为此操作设置了一个新的 NSManagedObjectContext,就像文档中所说的那样。

我认为问题是我在 ASINetworkQueue 完成并调用委托选择器时保存数据。由于在主线程上调用委托,因此保存消息失败。

这可能是问题所在吗?有人有解决方案吗?

I want to use the ASINetworkQueue inside an NSOperation. This works great and makes no problem. What fails is saving Core Data. I set up a new NSManagedObjectContext for this Operation like it is told in the docs.

I think that the problem is that I save the data when the ASINetworkQueue finishes and delegate selector is called. Because the delegates are called on the mainThread, the save message fails.

Can this be the problem and does anybody has a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑叹一世浮沉 2024-12-28 16:44:45

您正在使用 PerformSelectorOnMainThread 方法吗(合并新实例化的 ManagedObjectContext 中的更改)?

我在我的操作中做了类似的事情(ctx是我实例化的MOC):

首先注册通知:

// Register context with the notification center
        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 

        [nc addObserver:self
               selector:@selector(mergeChanges:) 
                   name:NSManagedObjectContextDidSaveNotification
                 object:ctx];

然后当您需要保存上下文时:

if ([ctx hasChanges]) {
            error = nil;

            // Save the context.
            if (![ctx save:&error])
            {
                // Do something with the error
            }

            // Clear out the scratchpad
            [ctx reset];

        }

然后是与主MOC合并的方法:

- (void)mergeChanges:(NSNotification *)notification
{
    id appDelegate = [[UIApplication sharedApplication] delegate];  

    NSManagedObjectContext *mainContext = [appDelegate managedObjectContext];
    // Merge changes into the main context on the main thread
    [mainContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) 
                                  withObject:notification
                               waitUntilDone:NO];
    // NSLog(@"Merged Changes");
}

希望这有帮助

You are using the PerformSelectorOnMainThread method right (to merge the changes from the new instantiated ManagedObjectContext)?

I do something like this in my Operations (ctx is my instantiated MOC):

First register for notifications:

// Register context with the notification center
        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 

        [nc addObserver:self
               selector:@selector(mergeChanges:) 
                   name:NSManagedObjectContextDidSaveNotification
                 object:ctx];

Then when you need to save the context:

if ([ctx hasChanges]) {
            error = nil;

            // Save the context.
            if (![ctx save:&error])
            {
                // Do something with the error
            }

            // Clear out the scratchpad
            [ctx reset];

        }

And then the method that does the merging with the main MOC:

- (void)mergeChanges:(NSNotification *)notification
{
    id appDelegate = [[UIApplication sharedApplication] delegate];  

    NSManagedObjectContext *mainContext = [appDelegate managedObjectContext];
    // Merge changes into the main context on the main thread
    [mainContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) 
                                  withObject:notification
                               waitUntilDone:NO];
    // NSLog(@"Merged Changes");
}

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文