kastiglione / Parse-RACExtensions

ReactiveCocoa for Parse
MIT License
61 stars 9 forks source link

[pfobject rac_save] is not fired #9

Closed bangedorrunt closed 11 years ago

bangedorrunt commented 11 years ago

Hi @kastiglione, I'm trying to save an PFObject after modify its data. But some how rac_save is not fired when I subscribe to that signal:

[[obj rac_save] subscribeNext:^ (id x) {
    //Do stuff here
}];

Updated: subscribeCompleted is working in my case

mhupman commented 11 years ago

The rac_save method returns a signal that will only complete or error since it uses the PFRACBooleanCallback. [PFObject save] either succeeds or fails - there's no "value" that makes sense to send across the signal.

bangedorrunt commented 11 years ago

Thanks @mhupman for your confirmation, as I looked at PFObject+RacExtensions.h header file, it said

/// Saves the PFObject. /// /// @see -saveInBackgroundWithBlock: /// /// @return A signal that completes on successful save.

So I did expect a returned BOOL flag. In this case how would I catch the error signal? Because I want to have an alert view that show warning message when saving PFObject to Parse unsuccessfully

mhupman commented 11 years ago

You won't get a BOOL result back explicitly, but it will be implied by the signal's behavior. If your signal gets a completed event, you can assume the save was successful (success == YES). However, if your signal gets an error event, the save was unsuccessful (success == NO) and the error parameter will contain details of the failure.

Use subscribeError:completed: if you want to observe both success and failure of the rac_save and get access to the underlying Parse error.

bangedorrunt commented 11 years ago

Tks @mhupman, I got my issue sorted out by using subscribeError:completed:, my last question is there are possibilities that I would merge some rac_save signals, what kind of value rac_save would return? or I just simple declare ^ (id x) for each returned signal value?

mhupman commented 11 years ago

If I understand your question correctly, you want to know when 2 or more rac_saves finish or error. You can use [RACSignal merge:] to accomplish that. Example:

RACSignal *save1 = [pfObject1 rac_save];
RACSignal *save2 = [pfObject2 rac_save];

[[RACSignal merge:@[save1, save2]] subscribeError:^(NSError *error){
// One of the saves encountered an error
} completed^{
// Both objects saved successfully
}];
bangedorrunt commented 11 years ago

How about

RACSignal *save1 = [pfObject1 rac_save];
RACSignal *save2 = [pfObject2 rac_save];
[[[RacSignal zip:@[save1, save2]] reduce:^(id x, id y) {
   // Do stuff here
}] flatten] subscribeCompleted:^ {
   // Do other stuff here
}];

What's your though of data type x and y???

Edited: For correct example

mhupman commented 11 years ago

As I stated above, the signal returned by rac_save will never send any values (by design). subscribeNext:, map:, flattenMap: and their ilk aren't of any use here since they only really make sense if we expect the signals in question to send values along. In this case, [RACSignal merge:] just aggregates the error and completed events of the signals passed into it, it won't inject any values into the stream.

If you explained what you are trying to do I could try to provide more accurate guidance.

bangedorrunt commented 11 years ago

Really appreciate your caring and quick response :+1: . I totally understand now. It's just because I'm learning both ReactiveCocoa & Parse-RacExtensions, so there is a gap between them that I need to understand to catch up :-) Again, thank you heaps @mhupman :star:

kastiglione commented 11 years ago

@mhupman :ok_hand:

@babygau If the circumstances dictate, and you need -rac_save to return a value for further operation composition, you can use -concat:.

For example:

[[object rac_save] concat:[RACSignal return:@YES]];
// or
[[object rac_save] concat:[RACSignal return:@object]];

This applies to any complete-or-error signal.