MagicalRecords在后台线程中持续存在图像获取?

时间:2013-02-14 03:01:30

标签: ios objective-c core-data magicalrecord

我有以下代码:

dispatch_async(dispatch_get_main_queue(), ^{
    NSManagedObjectContext *localContext = [NSManagedObjectContext contextForCurrentThread];

    Item *newItem = [Item createInContext:localContext];
    newItem.title = NULL_TO_NIL([itemJson valueForKey:@"title"]);
    newItem.image_url = NULL_TO_NIL([itemJson valueForKey:@"image_url"]);
    newItem.order_id = @([[self largestOrderId] intValue] + 1);

    NSURL *url = [NSURL URLWithString:newItem.image_url];
    NSData *data = [[NSData alloc] initWithContentsOfURL: url];
    if (data == nil) {
        NSLog(@"Image data is nil from %@", url);
    } else {
        NSLog(@"Image fetched in saveItemFromJson for cid:%@ order_id:%@", newItem.cid, newItem.order_id);
        newItem.image = [UIImage imageWithData:data];
    }

    if (![localContext hasChanges]) {
        NSLog(@"No local change detected. Quitting");
        return;
    }

    [localContext saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
        if (!success)
            NSLog(@"Error: %@", [error localizedDescription]);
        else
            NSLog(@"Item persisted for cid:%@ order_id:%@", newItem.cid, newItem.order_id);
    }];
});

我似乎得到了很多以下内容:

2013-02-13 18:55:47.404 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8386a90) → Saving <NSManagedObjectContext (0x8386a90): *** DEFAULT ***> on *** MAIN THREAD ***
2013-02-13 18:55:47.404 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8386a90) → Save Parents? 1
2013-02-13 18:55:47.404 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8386a90) → Save Synchronously? 0
2013-02-13 18:55:47.497 Giordano.iPhone[13956:c07] Image fetched in saveItemFromJson for cid:7218 order_id:10
2013-02-13 18:55:47.497 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8386a90) → Saving <NSManagedObjectContext (0x8386a90): *** DEFAULT ***> on *** MAIN THREAD ***
2013-02-13 18:55:47.498 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8386a90) → Save Parents? 1
2013-02-13 18:55:47.498 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8386a90) → Save Synchronously? 0
2013-02-13 18:55:47.499 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x8386a90) Context DEFAULT is about to save. Obtaining permanent IDs for new 10 inserted objects
2013-02-13 18:55:47.501 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) → Saving <NSManagedObjectContext (0x8385aa0): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
2013-02-13 18:55:47.502 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) → Save Parents? 0
2013-02-13 18:55:47.502 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) → Save Synchronously? 1
2013-02-13 18:55:47.502 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x8385aa0) Context BACKGROUND SAVING (ROOT) is about to save. Obtaining permanent IDs for new 10 inserted objects
2013-02-13 18:55:47.505 Giordano.iPhone[13956:c07] __70-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:]_block_invoke21(0x8385aa0) → Finished saving: <NSManagedObjectContext (0x8385aa0): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
2013-02-13 18:55:47.505 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING

出现以下错误消息:

2013-02-13 18:55:47.511 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING
2013-02-13 18:55:47.512 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING
2013-02-13 18:55:47.512 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING
2013-02-13 18:55:47.512 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING
2013-02-13 18:55:47.513 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING


2013-02-13 18:55:47.515 Giordano.iPhone[13956:c07] Error: (null)
2013-02-13 18:55:47.515 Giordano.iPhone[13956:c07] Error: (null)
2013-02-13 18:55:47.515 Giordano.iPhone[13956:c07] Error: (null)

我正在避免使用saveInBackgroundWithBlock,因为它已被弃用(文档需要更新?)

我的代码有什么问题吗?

更新

我的团队已经决定MagicalRecord现在太麻烦了。我们已经完全将我们的代码从MR迁移回CoreData。感谢您的关注。

3 个答案:

答案 0 :(得分:2)

我有同样的问题!只有当我在没有MR的情况下手动保存上下文时才修复它。

这是我的解决方案:

NSManagedObject + MyCategory.h

+ (void)saveDataInBackgroundWithBlock:(void(^)(NSManagedObjectContext *localContext))saveBlock
                           completion:(void(^)(void))completion;

+ (NSManagedObjectContext *)newMergableBackgroundThreadContext;

- (void)saveWithCompletion:(void(^)(void))completion;

的.m

+ (NSManagedObjectContext *)newMergableBackgroundThreadContext {
    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    context.parentContext = [self mainThreadContext];
    [context.userInfo setObject:[NSNumber numberWithInteger:VKCoreDataManagedObjectContextIDTempBackground]
                         forKey:@"contextID"];
    [context.userInfo setObject:kVKCoreDataManagedObjectContextBackgroundTemp
                         forKey:@"contextDebugName"];
    VKDLog(@"* New mergable backround context created! *");
    return context;
}

+ (void)saveDataInBackgroundWithBlock:(void (^)(NSManagedObjectContext *))saveBlock completion:(void (^)(void))completion {
    NSManagedObjectContext *tempContext = [self newMergableBackgroundThreadContext];
    [tempContext performBlock:^{

        if (saveBlock) {
            saveBlock(tempContext);
        }

        if ([tempContext hasChanges]) {
            [tempContext saveWithCompletion:completion];
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (completion) {
                    completion();
                }
            });
        }
    }];
}

- (void)saveWithCompletion:(void(^)(void))completion {
    [self performBlock:^{
        NSError *error = nil;
        if ([self save:&error]) {
            NSNumber *contextID = [self.userInfo objectForKey:@"contextID"];
            if (contextID.integerValue == VKCoreDataManagedObjectContextIDMainThread) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (completion) {
                        completion();
                    }
                });
            }
            [[self class] logContextSaved:self];
            if (self.parentContext) {
                [self.parentContext saveWithCompletion:completion];
            }
        } else {
            [VKCoreData handleError:error];
            dispatch_async(dispatch_get_main_queue(), ^{
                if (completion) {
                    completion();
                }
            });
        }
    }];
}

这里是使用样本:

[NSManagedObjectContext saveDataInBackgroundWithBlock:^(NSManagedObjectContext *localContext){
// do your stuff with local context
} completion:^{
// handle completion, update UI or something
}];

答案 1 :(得分:2)

几周前我使用MR时遇到过类似的问题。最后我决定抛弃它并自己做所有事情。对你的问题不是一个真正的解决方案,但我放弃它的原因是合理的。你可能需要去看看MR的实际来源。很大一部分文档是不正确的,只有通过阅读源才能学习相当数量的库。尝试使用MR进行后台处理时很可能存在问题。

如果您的代码适用于iOS6 +,那么您应该只使用Master-Main-Child上下文设置。使用dispatch_async也可能是个问题。让Core Data使用[NSManagedContext performBlock:]方法管理您的线程可能是一个更安全的世界。

如果您使用的是iOS5或更低版本,则Child上下文和performBlock:代码不起作用。最简单的解决方案是将Core Data保留在线程之外。在进入新线程/块之前从Core Data中提取任何所需信息。将该数据传递到您的块中并执行任何必要的处理。然后将它返回到某个字典/对象中的主线程,并在那里执行Core Data保存。

另外还有一个注意事项,我因为下载图像并将它们保存到Core Data而遇到了这个问题,你可能想看看我在过去几周中提出/解决的这些问题。可能会让你以后拉出一些头发:

Can I access the files used for external binary storage in Core Data?

最终导致我使用自己的存储机制而不是Core Data for files,这最终导致我遇到这个问题/解决方案:

Files are no longer readable after updating application to newest version

答案 2 :(得分:0)

您是否尝试过MR_saveOnlySelfWithCompletion: