如何实施云代码?

时间:2015-12-29 01:32:52

标签: ios swift parse-platform cloud-code

我对云代码很新,我遇到了麻烦。我已经提到了这个How to prevent duplicate entry on parse?,我正在尝试做同样的事情。我试图阻止我班上的重复录入。我不断收到云代码中的错误“Song已经存在”,即使该类为空。

这是云代码。我对querySongs.equalTo("title", newEntrySong.get("title"));正在做什么感到困惑。它在寻找实际的单词标题吗?因为我需要它来找到一个特定的标题。

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

var newEntrySong = request.object;

var querySongs = new Parse.Query("Pointer");
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();
          }
    });
 });

下面是我在类中查找重复项的代码,但它不能正常工作。我需要更换整件东西吗?还是摆脱它?因为我有云代码

 func getNowPlayingItem() {

    print("entered function")
    NSNotificationCenter.defaultCenter().removeObserver(self)

    if  let nowPlaying = musicPlayer.nowPlayingItem  {
        let title = nowPlaying[MPMediaItemPropertyTitle] as? String
        let artisttest = nowPlaying[MPMediaItemPropertyTitle]
        if let artist = nowPlaying[MPMediaItemPropertyArtist] as? String{

            let objectPointer = PFObject(className: "Pointer")
            let object = PFObject(className: "MasterSongs")


            let query = PFQuery(className: "Pointer")
            query.findObjectsInBackgroundWithBlock({
                (objects: [AnyObject]?, error: NSError?) -> Void in
                var objectIDs = objects as! [PFObject]

                for i in 0...objectIDs.count-1{
                    self.Parsearray.append((objectIDs[i].valueForKey("title") as? String)!)

                }


                if self.Parsearray.contains(title!){
                    print("already in db")
                       NSNotificationCenter.defaultCenter().addObserver(self, selector: "getNowPlayingItem", name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification, object: nil)
                }else{
                    objectPointer["title"] = title
                    objectPointer["user"] = PFUser.currentUser()
                    objectPointer["artist"] = artist
                    print("about to save with artist")
                    objectPointer.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
                        if(error != nil){
                            print(error)
                        }else{
                             NSNotificationCenter.defaultCenter().addObserver(self, selector: "getNowPlayingItem", name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification, object: nil)
                            print("saved")
                        }

                    })



                }

            })




        }else{


            let objectPointer = PFObject(className: "Pointer")


            let query = PFQuery(className: "Pointer")
            query.findObjectsInBackgroundWithBlock({
                (objects: [AnyObject]?, error: NSError?) -> Void in
                var objectIDs = objects as! [PFObject]

                for i in 0...objectIDs.count-1{
                    self.Parsearray.append((objectIDs[i].valueForKey("title") as? String)!)

                    // print(self.Parsearray)
                }


                if self.Parsearray.contains(title!){
                    print("already in db with no artist")
                       NSNotificationCenter.defaultCenter().addObserver(self, selector: "getNowPlayingItem", name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification, object: nil)
                }else{
                    objectPointer["title"] = title
                    objectPointer["user"] = PFUser.currentUser()
                    objectPointer["artist"] = "No artist found :("
                    print("about to save")
                        objectPointer.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in

                            if(error != nil){
                                print(error)
                            }else{
                                NSNotificationCenter.defaultCenter().addObserver(self, selector: "getNowPlayingItem", name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification, object: nil)
                                print("saved")
                            }

                    })


                }

            })





        }


        }

}

1 个答案:

答案 0 :(得分:1)

OP的beforeSave函数无法检查查询是否发现重复。如上所述,它总是报告重复,但可以简单地修复......

var newEntrySong = request.object;
var querySongs = new Parse.Query("Pointer");
querySongs.equalTo("title", newEntrySong.get("title"));
// whatever else you want to do that defines a duplicate

querySongs.first().then(function(result) {
    // this is the check the code was missing
    if (result) {
        response.error({errorCode:123,errorMsg:"Song already exist!"}); 
    } else {
        response.success();
    }
}, function(error) {
    response.error(error);
})

如果您正在努力防止重复,那么客户端代码也不需要这样做。