2015-02-11 51 views
2

我是新手在iOS中使用Parse我作出這樣的包含Facebook登錄的應用我想打,如果用戶已經登錄,然後我把它的數據在我的解析數據表等作爲如何避免解析iOS中的數據重複?

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); 
      } 

     }]; 
    } }]; 

但如上述工作一樣,在這裏我想打,如果用戶已經登錄,然後它的數據是不是在插入表中每時間。 請給我這個解決方案。

+0

你們是不是不保存重複的用戶或者是你想不保存重複的對象?我很困惑你的示例解析已經有一個用戶系統,你不需要手動管理。 – CubanAzcuy 2015-07-15 20:23:39

回答

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 
    }]; 
}