0

I am using Parse and PFUser in a Swift iOS app, and find myself in a case where PFUser.current() does not do exactly what I want, due to synchronisation issues.

For that reason I am trying to use: PFUser.getCurrentUserInBackground().

I got started with the code below, inspired from what can be found here: https://github.com/BoltsFramework/Bolts-ObjC.

But this document probably being a bit outdated, it does not quite work.

let userCheckTask = PFUser.getCurrentUserInBackground()
userCheckTask.continueWith {
    (task: BFTask!) -> BFTask<AnyObject> in
    if task.isCancelled() { // Error-1.
        // the save was cancelled.
    } else if task.error != nil {
        // the save failed.
    } else {
        // the object was saved successfully.
        var object = task.result() as PFObject // Error-2.
    }
}

The compiler gives me two errors, this one on the line marked "Error-1"

Cannot invoke 'isCancelled' with no arguments

And this other one on the line marked "Error-2"

Expression type 'PFUser?' is ambiguous without more context

I have no idea what kind of argument 'isCancelled' is expecting.

Does anyone know how to fix those?

William George
  • 6,735
  • 3
  • 31
  • 39
Michel
  • 10,303
  • 17
  • 82
  • 179

1 Answers1

1
let userCheckTask = PFUser.getCurrentUserInBackground()
userCheckTask.continueWith {
    (task: BFTask) -> BFTask<AnyObject> in
    if let e = task.error {
        return BFTask(error: e)
    } else {
        return BFTask(result: task.result)
    } 
}
Sachin Vas
  • 1,757
  • 12
  • 16
  • I indeed do not get the first error replacing as you do "task.isCancelled()" by "task.isCancelled" , but I get another one using your code: "Missing return in a closure expected to return 'BFTask'". – Michel Mar 18 '19 at 09:12
  • Yeah, you have to return the PFUser object. – Sachin Vas Mar 18 '19 at 09:13
  • What is the instruction for that? "return ???". In other words what to you mean by "PFUser object" in this context? – Michel Mar 18 '19 at 09:34
  • OK. Thanks, at least now it compiles. I need to try and look closer to see if that solves my problem. – Michel Mar 18 '19 at 09:52
  • I have tried the code, but it does not do much good that I can see. It goes through the success branch even before I have time to log in. Since I was counting on that to get the credential information, that doesn't work as I expected. Or I am doing something wrong. – Michel Mar 18 '19 at 16:16
  • Can you post your full code? It's not clear on how you trying to achieve. – Sachin Vas Mar 19 '19 at 05:47
  • I was trying to use PFUser.getCurrentUserInBackground to get the login information asynchronously because calling PFUser.current() was giving me wrong results on the first call. I still do not know how to use PFUser.getCurrentUserInBackground properly, but I found out that if I call PFUser.current() at a different timing (precisely in viewDidAppear instead of viewDidLoad), it gives me correct results. And this solves my current problem. – Michel Mar 19 '19 at 06:03