I am trying to establish a one to many relationships between 1 User(User Table) and his Contacts(Usercontacts Table)
I am establishing the relationship likewise : calling this way, [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);
}];
}
}
The problem is it is taking lot of time for uploading to stackmob and My App is getting struck in the view. Please see my code and suggest me the best option i can do.