0

编辑

好吧,我想我明白了。

我的整体操作流程是这样的:

-(void)submitRegistration
{    
    //[[AppDelegate sharedDelegate] drupalLogin];

    [self validateFields];

    if(fieldsValid)
    {
        [self createBike];

        // save member node will save bike node too if successful
        // bike node will not be saved if member node did not save successful
        [self saveMemberNode];

        // save the updated member node and bike node to Core Data
        [self saveLocalRegistration]; 
        // *** ABOVE LINE GETS EXECUTED BEFORE ASYNC REQUEST FINISHES ***
    }
}

我的“[self saveLocalRegistration];” 代码在异步请求“块”完成执行之前执行,导致它崩溃。

“saveLocalRegistration”方法调用[managedObjectContext save:&error];

原始问题

刚开始我第一次潜入核心数据。

在我的应用程序中,我想做的一件事是将一些数据发送到 Drupal 服务器。

我已经将问题缩小到这个:

// *** *** *** 
// doesn't crash here
// *** *** ***
NSLog(@"bike.brand = %@", bike.brand);
NSLog(@"bike.model = %@", bike.model);
NSLog(@"bike.features = %@", bike.features);

// save new member node to Drupal
[DIOSNode nodeSave:memberData success:^(AFHTTPRequestOperation *operation, id responseObject) 
 {
     NSLog(@"Node saved!");
     NSDictionary *serverNodeData = (NSDictionary *)responseObject;

     NSLog(@"Server node details = %@", serverNodeData);

     // set the member's memberId to the newly created Drupal member node's nodeId
     member.memberId = [NSNumber numberWithLong:[[serverNodeData valueForKey:@"nid"] longLongValue]];

     // *** *** ***
     // accessing bike properties here inside this objective c "Block" crashes app
     // somehow the above line "member.memberId = .... " doesn't crash despite both being Core Data entities
     // *** *** ***
     NSLog(@"bike.brand = %@", bike.brand); // <--- CRASH HERE ***
     NSLog(@"bike.model = %@", bike.model);
     NSLog(@"bike.features = %@", bike.features);

     //bike.ownerId = member.memberId;

     // save bike node with member id
     [self saveBikeNode];
 } 
           failure:^(AFHTTPRequestOperation *operation, NSError *error) 
 {
     NSLog(@"Failed to save node, error: %@", [error localizedDescription]);
 }];

从上面的源代码可以看出,第一个 NSLogs 块并没有使应用程序崩溃,但在目标 c“块”内,同一块 NSLogs 使应用程序崩溃。

注意:我相信“[DIOSNode nodeSave:memberData success:....”Objective C 块是一个异步请求。不知道这是否是多线程问题。

bike.brand、bike.model 和bike.features 是“字符串”数据类型。

4

2 回答 2

0

我可以通过在崩溃前查看调试器的状态来确认这一点,但我的理解是您正在从一个不属于它的线程访问一个托管对象。

您必须在托管对象上下文设计队列(performBlock 和 performBlockAndWait)上安排成功块。

于 2012-06-20T08:20:49.200 回答
0

在块外声明它并在你的块内使用它而不是'self'

__block <classname> *blocksafeSelf = self;
于 2012-06-20T05:45:00.763 回答