0

I have the following code:

    func compareDate() {
    let query = PFQuery(className: "DailyBDate")
    let calendar2 = NSCalendar.currentCalendar()
    let date2 = NSDate()
    let components2 = calendar2.components([.Day], fromDate: date2)
    let day2 = components2.day as Int


    query.whereKey("User", equalTo: self.username)
    let DailyBonusDate = query.selectKeys(["Date"])
    query.findObjectsInBackground()


    // seems like its always something else... :/
    if(day2 != DailyBonusDate) {
        self.Dailybonus = 0;
        self.Likelimit = 0;
        self.DailybonusDefault.setValue(self.Dailybonus, forKey: "Dailybonus")
        self.DailybonusDefault.synchronize()
        self.LikelimitDefault.setValue(self.Likelimit, forKey: "DailyBonus")
        self.LikelimitDefault.synchronize()
    }
}

I have a Parse class called DailyBDate. In that class I have one column for the username and one column for the date where only the days are saved.

I want to compare the date which is saved in Parse with the current date (day2 variable). If these aren't the same (which would mean that the user logged-in in another day) swift will set the variable Dailybonus and Likelimit to 0. But if the date is the same it should stay. I tried the app but at every launch it sets the 2 variables dailybonus and Likelimit to 0 even thoogh the Date is the same. I think the problem is that you cannot compare the value saved in parse directly with a local variable.

/ Edit: Added a photo of the parse database. Parse Database

1 Answer 1

1

When you call anything with findObjectsInBackground() like this, the ensuing lines of code will execute immediately because of the asynchronous nature of this call. That means that when you then try to compare the date to what was "received" from parse, it will never be equal because it's most likely that the data hasn't been received from the server yet. Instead, you should use this call with a completion handler like so:

query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
    if error == nil && objects?.count != 0 {
        let dailyBonusDate = objects[0] as? NSDate
        if(day2 != DailyBonusDate) {
            self.Dailybonus = 0;
            self.Likelimit = 0;
            self.DailybonusDefault.setValue(self.Dailybonus, forKey: "Dailybonus")
            self.DailybonusDefault.synchronize()
            self.LikelimitDefault.setValue(self.Likelimit, forKey: "DailyBonus")
            self.LikelimitDefault.synchronize()
        }
    }
    else {
        an error occurred or there were no results
    }
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer but this isnt really what i wanted to have. I uploaded a picture, maybe this could help you out understanding what i mean :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.