Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

ios - How to prevent duplicate entry on parse?

I' trying to save song info to parse, but if the song already exist in parse I want my code just do nothing.

I've tried this code below:

var Music = PFObject(className:"Musics")
        var query = PFQuery(className:"Musics")
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in

            if error == nil {
                // The find succeeded.
                println("Successfully retrieved (objects!.count) scores.")
                // Do something with the found objects
                if let objects = objects as? [PFObject] {
                    for object in objects {


                        var songTitle = object.objectForKey("songTitle") as? String

                        if songTitle != title {

                            Music["createdBy"] = PFUser.currentUser()
                            Music["songTitle"] = title
                            Music["albumCover"] = imageFile
                            Music["songArtist"] = artist
                            Music.saveInBackgroundWithBlock {

                                (success: Bool, error: NSError?) -> Void in
                                if (success) {

                                    println("succeed")

                                } else {
                                    // There was a problem, check error.description
                                    println("error jeh")
                                }
                            }

                        }else{

                            println("song already exist")

                        }

                    }
                }
            } else {
                // Log details of the failure
                println("Error: (error!) (error!.userInfo!)")
            }
        }

the code above give below result on log:

Successfully retrieved 4 scores. song already exist Successfully retrieved 4 scores. song already exist Successfully retrieved 4 scores. song already exist Successfully retrieved 4 scores. song already exist succeed succeed succeed succeed succeed succeed succeed succeed succeed succeed succeed succeed

Why my for loop , looping more than the Objects.count? and how can I prevent dupiclate entry on parse?

give me any advice, doesn't matter in obj c or swift

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suggest to implement a simple beforeSave trigger, on Parse Cloud code, in order to check if the new entry song already exist (basically you're going to make one or more field uniques. For example:

Parse.Cloud.beforeSave("Musics", function(request, response) {

  var newEntrySong = request.object;

  var querySongs = new Parse.Query("Musics");
  querySongs.equalTo("title", newEntrySong.get("title"));
  querySongs.equalTo("description", newEntrySong.get("description"));

  // this could be a sort of signature for your song, to make more unique (skipping spaces and new lines for example)
  querySongs.equalTo("md5Title", newEntrySong.get("md5Title"));
  querySongs.first({
    success: function(temp) {
        response.error({errorCode:123,errorMsg:"Song already exist!"});          
    },
    error: function(error) {
      response.success();
    }
  });
});

Hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...