Search code examples
iosobjective-cparse-platformpfquerypfobject

Wait saveInBackground Parse before viewDisappear


I would like to wait saveInBackground Parse method before the view disappears. Because the view following is using this data, but that doesn't have the time to refresh...

Here is my code :

-(void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    PFQuery *urlImage = [PFQuery queryWithClassName:@"urlImage"];
    [urlImage whereKey:@"objectId" equalTo:@"IcK6mFChL7"];
    [urlImage getFirstObjectInBackgroundWithBlock:^(PFObject *urlImageParse, NSError *error) {
        if (!error) {
            [urlImageParse setObject:self.photoURL.text forKey:@"URL"];
            [urlImageParse saveInBackground];
        } else {
            NSLog(@"Error: %@", error);
        }}];
}

Can I make this code in other place that viewWillDisappear:? Or maybe use MBProgressHUD?


Solution

  • viewWillDisappear is a place to execute code, knowing that the view is about to disappear. I believe what you are looking to do is run some action, prior to the view actually going away.
    This part right here -> [urlImage getFirstObjectInBackgroundWithBlock:^ means that the code inside that block is going to run on a different thread. So what you are currently saying is, when the view is about to disappear, spin off this other thread and save something in the background, but continue on doing whatever you need to do, like make this view disappear. That's what you're telling the system. So it continues on, doing what it was going to do, not caring about the results of saving that object.
    There is a reason that your view is about to disappear. Something happened in your app where the system is thinking it needs to close this view and present another one. It could be that you hit a back button, it could be that you clicked a save button, and at the end of that code, you are asking the system to pop this view off the stack. We really don't know with only the code you posted. What we can assume though, is whatever action was taken to make this view disappear, is where you should be trying to save this object, and you should probably be waiting for a response before you leave this view. If the next view is dependent on that information, then it doesn't make sense to dismiss this view, and present the next one, until you get a successful response that this object was saved. This is just an assumption though, since I don't understand what your app is doing or what is going on when you are trying to save this data, and what the next view looks like. I would say that typically, if you need this stuff to be saved, in order to continue on in your app, you should be saving this data and waiting for a response, to either display an error message to the user, or if successful, then move on to the next screen.