在MOC中使用obtainPermanentIDsForObjects,performBlock会导致奇怪的错误

时间:2012-08-20 12:42:32

标签: cocoa-touch core-data

我使用在线程中使用MOC的新方式

问题是,当我使用呼叫时

[child obtainPermanentIDsForObjects:parsedItemsObjects error:&error];

获取pernament ID ...

离开街区的时候,我得到了

NSManagedObject Context Queue Serial
i get an EXC_BAD_ACCESS on objc_msgSend

libobjc.A.dylib`objc_msgSend:
0x34eb9f68:  teq.w  r0, #0
0x34eb9f6c:  beq    0x34eb9faa               ; objc_msgSend + 66
0x34eb9f6e:  push.w {r3, r4}
HERE 0x34eb9f72:  ldr    r4, [r0]
0x34eb9f74:  lsr.w  r9, r1, #2
0x34eb9f78:  ldr    r3, [r4, #8]
...

代码

- (void) parse:(NSDictionary *)serviceResponseDictionary onParseFinished:(ParseFinished)onParseFinished onParseFailed:(ParseFailed)onParseFailed
{
    NSManagedObjectContext * parent = [NSManagedObjectContext MR_defaultContext]; // NSMainQueueConcurrencyType
    NSManagedObjectContext * child = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [child setParentContext:parent];

    [child performBlock:^(){

        Class parsedItemsClass = [NSNull class]; 
        NSMutableArray * parsedItemsObjectIDs;
        NSMutableArray * parsedItemsObjects;

        // processing the serviceResponseDictionary here 
        // creating / deleting / modifying Managed objects in child here

        NSError * error;
        [child save:&error];
        // handle and check error here 

        [child obtainPermanentIDsForObjects:parsedItemsObjects error:&error];
        // handle and check error here 

        // obtain pernament IDs here, if remove, it doesnt crash
        parsedItemsObjectIDs = [[NSMutableArray alloc] initWithCapacity:[parsedItemsObjects count]];

        for (NSManagedObject * mo in parsedItemsObjects) {
            [parsedItemsObjectIDs addObject:mo.objectID];
        }

        [parent performBlock:^(){

            NSError * error;
            [parent save:&error];
            // handle and check error here 

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                onParseFinished(parsedItemsClass, parsedItemsObjectIDs);
            }];
        }];
    }];
}

0 个答案:

没有答案