2014-12-24 65 views
0

如何避免不同的用戶將同一個字符串保存到同一列?如果對象已經創建,它需要檢查特定列...解析iOS對象複製

我不試圖爲PFUser類,以檢查重複的用戶,它的默認值。

任何代碼線索,開始的例子?

下面是我保存對象的代碼。字段_channelName是不能重複的字段,必須在保存前檢查。

- (IBAction)addButton:(id)sender { 

// Create a new Post object and create relationship with PFUser 
PFObject *newPost = [PFObject objectWithClassName:@"Channel"]; 
[newPost setObject:[_textView text] forKey:@"about"]; 
[newPost setObject:[_channelName text] forKey:@"name"]; 
[newPost setObject:[_enterKey text] forKey:@"enterkey"]; 
[newPost setObject:[PFUser currentUser] forKey:@"owner"]; // One-to-Many relationship created here! 

NSData *imageData = UIImageJPEGRepresentation(self.channelImage.image, 0.8); 

PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:imageData]; 
[newPost setObject:imageFile forKey:@"image"]; 

// Set ACL permissions for added security 
PFACL *postACL = [PFACL ACL]; 
[postACL setPublicReadAccess:YES]; 
[PFACL setDefaultACL:postACL withAccessForCurrentUser:YES]; 
//PFACL *postACL = [PFACL ACLWithUser:[PFUser currentUser]]; 
[postACL setPublicReadAccess:YES]; 
[newPost setACL:postACL]; 


// Save new Post object in Parse 
[newPost saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
    if (!error) { 
     [self dismissViewControllerAnimated:YES completion:nil]; 
     // Dismiss the viewController upon success 

    } else { 

     //implement upload error message 
    } 
    }]; 

    [self dismissViewControllerAnimated:YES completion:nil]; 

}

感謝名單和歡呼!!!!

+1

你可以發佈一個示例對象嗎?默認情況下,Parse會在每次實例化並保存一個對象時創建一個新對象,除非您已查詢或特別引用某些對象。代碼示例將幫助你:) –

+1

是的,當然沒有問題!只要我接近我的項目,我會在問題中發佈並再次與您聯繫。 –

+0

嘿@RyanKreager我編輯了我的問題。請試一試 –

回答

1

您可以通過兩種不同的方式完成此操作。您可以使用雲代碼,也可以在客戶端進行操作。

雲代碼會是這個樣子:

Parse.Cloud.beforeSave("YourClass", function(request, response) { 
var query = new Parse.Query("YourClass"); 
query.equalTo("uniqueColumn",request.object.get("uniqueColumn")); 
query.first({ 
    success: function(object) {    
     if (object){    
      var currentUser = request.object; 
      if (currentUser.id === object.id) { // if it already belongs to the user it's good 
       response.success(); 
      } else { // otherwise it already exists and it belongs to someone else 
       response.error("uniqueColumn exists"); 
      } 
     } else { // If the object doesn't exist at all it's available 
      response.success(); 
     } 
    }, 
    error: function(error) { 
     response.error("Could not validate uniqueness for this uniqueColumn."); 
    } 
});  
}); 

您可以通過使用findObjectsInBackgroundWithBlock方法和檢查,看看如果返回的數組上計數大於0。這樣看起來會是做客戶端像這樣:

PFQuery *query = [PFQuery ...]; 
[query whereKey:@"name" equalTo:[_channelName text]]; 
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    if (objects.count > 0) { 
     // Not unique, name already exist, handle it here 
    } else { 
     // Name is unique, proceed to save your object here 

     // Create a new Post object and create relationship with PFUser 
     PFObject *newPost = [PFObject objectWithClassName:@"Channel"]; 
     [newPost setObject:[_textView text] forKey:@"about"]; 
     [newPost setObject:[_channelName text] forKey:@"name"]; 
     [newPost setObject:[_enterKey text] forKey:@"enterkey"]; 
     [newPost setObject:[PFUser currentUser] forKey:@"owner"]; // One-to-Many relationship created here! 

     NSData *imageData = UIImageJPEGRepresentation(self.channelImage.image, 0.8); 

     PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:imageData]; 
     [newPost setObject:imageFile forKey:@"image"]; 

     // Set ACL permissions for added security 
     PFACL *postACL = [PFACL ACL]; 
     [postACL setPublicReadAccess:YES]; 
     [PFACL setDefaultACL:postACL withAccessForCurrentUser:YES]; 
     //PFACL *postACL = [PFACL ACLWithUser:[PFUser currentUser]]; 
     [postACL setPublicReadAccess:YES]; 
     [newPost setACL:postACL]; 

     [newPost saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
      if (!error) { 
       [self dismissViewControllerAnimated:YES completion:nil]; 
       // Dismiss the viewController upon success 

      } else { 

       //implement upload error message 
      } 
      }]; 

      [self dismissViewControllerAnimated:YES completion:nil]; 
     } 
    } 
}]; 

//... 
+0

Hi @SuperKevin感謝您的回覆。我認爲在客戶端實施這個對我來說會更好。我在問題中發佈了我當前的代碼,將對象保存到Parse。字段_channelName是不能重複的字段。在上面的客戶端示例中,我應該插入PFQuery部分?成功的條件裏面對嗎? thnx和歡呼! –

+0

我用新發布的代碼更新了代碼,但我會建議去雲代碼方向。它更乾淨。 – SuperKevin

+0

哇感謝隊友!希望它也能幫助別人。雲代碼的性能/時間最好?是否延遲?使用雲代碼將有必要改變客戶端的任何代碼?如何在客戶端調用該雲?解析自動檢測到它? –