如何在Parse iOS中避免数据复制?

时间:2015-02-11 08:54:10

标签: ios facebook parse-platform

我是iOS中的新手,使用Parse我制作一个包含Facebook登录的应用程序,如果用户已登录,我想将其数据放入我的Parse Data表中,如

PFObject *userInfo = [PFObject objectWithClassName:@"User"];
        userInfo[@"email"]=self.email;
        userInfo[@"name"]=self.username;
        [userInfo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
         {
             if (succeeded)
             {
                 NSLog(@"Do whatever you want to Do");
             }
             else{
                 NSString *errorString = [[error userInfo] objectForKey:@"error"];
                 NSLog(@"Error: %@", errorString);
             }

         }];

但是它包含每个相同用户的重复数据,因此我为此编写了一个代码,如

PFQuery *query = [PFQuery queryWithClassName:@"User"];
[query whereKey:@"name" equalTo:self.username];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
    NSLog(@"%@",objects);
    if ([objects containsObject:self.username])
    {
        NSLog(@"Successfully retrieved: %@", objects);
    }
    else
    {
        PFObject *userInfo = [PFObject objectWithClassName:@"User"];
        userInfo[@"email"]=self.email;
        userInfo[@"name"]=self.username;
        [userInfo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
         {
             if (succeeded)
             {
                 NSLog(@"Do whatever you want to Do");
             }
             else{
                 NSString *errorString = [[error userInfo] objectForKey:@"error"];
                 NSLog(@"Error: %@", errorString);
             }

         }];
    }    }];

但它与上面的工作方式相同,这里我想知道如果用户已经登录,那么每次都没有将数据插入到表中。 请给我解决方案。

1 个答案:

答案 0 :(得分:0)

我不知道你想要做什么,但似乎你正在尝试创建自己的用户对象。我会使用内置的一个。链接到文档。再加上在facebook下面使用它的简单例子。它不包括一切。 https://www.parse.com/docs/ios/guide#users-logging-in

NSArray *permissions = @[kLoginPermissionPublicProfile, kLoginPermissionEmail, kLoginPermissionFitness, kLoginPermissionBirthday];

if ([PFUser currentUser] == nil) {
    if(facebookSignin){
        [PFFacebookUtils logInWithPermissions:permissions block:^(PFUser *user, NSError *error) {
            //Pull any facebook data and save it
        }];
    }else if(signUp){
        PFUser *user = [PFUser user];
        user.username = email;
        user.password = password;
        user.email = email;

        [user signUpInBackgroundWithBlock:^(BOOL success, NSError* error){
            //add data and save it
        }];
    }else{
        [PFUser logInWithUsernameInBackground:email password:password block:^(PFUser* user, NSError* error) {
            if (!error) {
                if (user) {
                    //add data and save it
                }
            }
        }];
    }
}else{
    PFUser *user = [PFUser currentUser];
    user[@"phone"] = @"415-392-0202";
    [userInfo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error){
        //ToDo
    }];
}