编辑
好吧,我想我明白了。
我的整体操作流程是这样的:
-(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 是“字符串”数据类型。