2015-07-13 276 views
0

如何防止重复输入?我目前正在使用这个代码,我不知道如何完成更多。如何防止Swift Parse iOS上的重复条目?

self.applicantClass = PFObject(className: "Applicants") 

    self.applicantClass["username"] = PFUser.currentUser()!.username 
    self.applicantClass["jobId"] = self.object["jobId"] as? String 


    self.applicantClass.saveEventually { (success, error) -> Void in 
     if (error == nil) { 
      println("saved") 
     }else { 
      println(error!.userInfo!) 
     } 
    } 

回答

1

在将数据发送给解析之前,您需要使用beforeSave方法进行验证。我没有代码,但在谷歌快速搜索发现:

https://www.parse.com/questions/unique-fields--2

希望有所帮助。

+2

我对Java一无所知。 :D –

+2

已解决问题。 :D谢谢。这真的帮助了我很多。 :* –

1

所以这就是我完成它的方式。

在main.js文件:

var Applicants = Parse.Object.extend("Applicants"); 

// Check if jobId is set, and enforce uniqueness based on the jobId column. 
Parse.Cloud.beforeSave("Applicants", function(request, response) { 
    if (!request.object.get("jobId")) { 
    response.error('A Applicants must have a jobId.'); 
    } else { 
    var query = new Parse.Query(Applicants); 
    query.equalTo("jobId", request.object.get("jobId")); 
    query.first({ 
     success: function(object) { 
     if (object) { 
      response.error("A Applicants with this jobId already exists."); 
     } else { 
      response.success(); 
     } 
     }, 
     error: function(error) { 
     response.error("Could not validate uniqueness for this Applicants object."); 
     } 
    }); 
    } 
}); 

我真的不认为我需要解释这一点,代码本身说话。 :D玩得开心。