0

我正在尝试在 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 在视图中被击中。请查看我的代码并建议我可以做的最佳选择。

4

1 回答 1

0

我建议启用缓存,有关详细信息,请参见此处。这样每次保存都不会导致服务器往返。如果您用于[SMCoreDataStore contextForCurrentThread]获取托管上下文,您还可以将此操作移动到后台队列。

于 2013-07-08T20:16:21.257 回答