应用程序在尝试在用户和用户联系人之间建立一对多关系时被击中(Stackmob)



我正在尝试在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需要很多时间,而我的应用程序在视图中受到了冲击。请查看我的代码,并建议我可以做的最佳选项。

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

最新更新