尝试在用户和用户联系人(Stackmob)之间建立一对多关系时应用程序被击中
Posted
技术标签:
【中文标题】尝试在用户和用户联系人(Stackmob)之间建立一对多关系时应用程序被击中【英文标题】:Application Struck while trying to establish one to Many Relationship between User & UserContacts (Stackmob) 【发布时间】:2013-07-04 12:42:51 【问题描述】:我正在尝试在 1 个用户(用户表)和他的联系人(用户联系人表)之间建立一对多的关系
我也在建立这种关系: 这样调用,[hypeController uploadContactsStackMob];
- (void)uploadContactsStackMob
NSDictionary *args1 = [[NSDictionary alloc] init];
NSLog([[NSUserDefaults standardUserDefaults] boolForKey:@"isFacebookClicked"] ? @" yes": @" No");
NSLog(@"ajsgd %@",[self.appDelegate coreDataStore]);
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isFacebookClicked"])
NSLog(@"context %@",self.managedObjectContext);
userObject = [[User alloc]initWithEntity:[NSEntityDescription entityForName:@"User" inManagedObjectContext:self.managedObjectContext] insertIntoManagedObjectContext:self.managedObjectContext];
[userObject setValue:firstName forKey:@"firstname"];
[userObject setValue:lastname forKey:@"lastname"];
[userObject setValue:emailAddress forKey:@"email"];
[userObject setValue:zipCodeTxtFld.text forKey:@"zipcode"];
[userObject setValue:@"password" forKey:@"password"];
[userObject setValue:dateofBirth forKey:@"dateofbirth"];
[userObject setValue:cellPhoneNumber forKey:@"phonenumber"];
[userObject setValue:gender forKey:@"salutation"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=normal", [userDict valueForKey:@"id"]]];
NSData *imageData = [NSData dataWithContentsOfURL:url];
NSString *imageString = [SMBinaryDataConversion stringForBinaryData:imageData name:@"profileImage.jpg" contentType:@"image/jpg"];
[userObject setValue:imageString forKey:@"profile_picture"];
[userObject setValue:[userObject assignObjectId] forKey:[userObject primaryKeyField]];
NSLog(@"context uploadContactsStackMob %@",userObject);
[self.managedObjectContext saveOnSuccess:^
NSLog(@"You created a new User object!");
onFailure:^(NSError *error)
NSLog(@"There was an error! %@", error);
];
NSLog(@"contactsObjectsArr %@",contactsObjectsArr);
NSLog(@"contactsArray %@",self.allcontactsArray);
for (int i = 0; i <[self.allcontactsArray count]; i++)
args1 = [self.allcontactsArray objectAtIndex:i];
Usercontacts *usercontactsObject = [NSEntityDescription insertNewObjectForEntityForName:@"Usercontacts" inManagedObjectContext:self.managedObjectContext];
[usercontactsObject setValue:[args1 valueForKey:@"contactDescription"] forKey:@"contactdescription"];
[usercontactsObject setValue:[args1 valueForKey:@"name"] forKey:@"name"];
[usercontactsObject setValue:[args1 valueForKey:@"phone"] forKey:@"phone"];
[usercontactsObject setValue:@"user_id" forKey:@"Usercontacts_id"];
[usercontactsObject setValue:[usercontactsObject assignObjectId] forKey:[usercontactsObject primaryKeyField]];
//
NSError *error = nil;
if (![self.managedObjectContext saveAndWait:&error])
NSLog(@"There was an error! %@", error);
else
NSLog(@"You created a Usercontact object!");
[contactsObjectsArr addObject:usercontactsObject];
NSLog([[NSUserDefaults standardUserDefaults] boolForKey:@"isSubmitClicked"] ? @"isSubmitClicked yes": @"isSubmitClicked No");
NSLog(@"contactsObjectsArr %@",contactsObjectsArr);
[userObject addUsercontactsObject:usercontactsObject];
[self.managedObjectContext saveOnSuccess:^
NSLog(@"You created a relationship between the User and Usercontact Objects!");
onFailure:^(NSError *error)
NSLog(@"There was an error! %@", error);
];
问题是上传到 stackmob 需要花费大量时间,并且 My App 在视图中被击中。请查看我的代码并建议我可以做的最佳选择。
【问题讨论】:
【参考方案1】:我建议启用缓存,详情请参阅here。这样每次保存都不会导致服务器往返。如果您使用[SMCoreDataStore contextForCurrentThread]
获取托管上下文,也可以将此操作移至后台队列。
【讨论】:
以上是关于尝试在用户和用户联系人(Stackmob)之间建立一对多关系时应用程序被击中的主要内容,如果未能解决你的问题,请参考以下文章