0

Just wonder if I can save parse PFObject in background and return it in callback instead of Bool.

parseObject.saveInBackground {
                  (success: Bool, error: Error?) in
                  if (success) {
                    completion(.success(true))
                  } else {
                    let error = NSError(domain: "OURTEAM", code: 0, userInfo: [NSLocalizedDescriptionKey: "Save invoice failure"])
                    completion(.failure(error))
                  }
                }

or do I need to re fetch object one more time from remote?

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • Yes. It is your code. You can declare the `Result` passed to `completion` as `Result` if you like. – Paulw11 Mar 14 '21 at 21:24
  • @Paulw11 thanks for comments but saveInBackground has this definition: `typedef void (^PFBooleanResultBlock)(BOOL succeeded, NSError *_Nullable error);` – Matrosov Oleksandr Mar 14 '21 at 21:31
  • 1
    You aren't changing `saveInBackground` - You are changing the signature of `completion` - which presumably was passed into the function that is calling `saveInBackground`, so `completion(.success(parseObject))` – Paulw11 Mar 14 '21 at 21:46
  • oh you mean the instance object I just saved in the background this is what I am looking for? – Matrosov Oleksandr Mar 14 '21 at 22:13

1 Answers1

1

I do not see why you would need the have that object in the callback when you already have the updated instance.

For example, the parseObject which you are saving is the updated instance once your callback completes, so just reference that.

You will see that after the save completes that parseObject which you previously had in memory has now been assigned an objected (assuming you created a new object and saved)

Tanzim Chowdhury
  • 3,020
  • 2
  • 9
  • 21