0

I have a little trouble understanding how this works. I am currently working with a sync table which uses calls like the following:

-(void)addItem:(NSDictionary *)item completion:(CompletionBlock)completion{
[self.syncTable insert:item completion:^(NSDictionary *result, NSError *error) {
    [self LogErrorIfNotNil:error];


}];

I understand how you can use a block as a parameter to execute some extra code within your function, for example with dispatch_async. but when it comes to this line

[self.syncTable insert:item completion:^(NSDictionary *result, NSError *error) {

result is here a dictionary including all extra columns that follows after "item" has been added to the table. Thinking about it,it seems that "result" is more like the resulting type of addItem: instead of being a parameter of the method (due to being the result of the executed method)

EDIT: Basically, I don't understand where the NSDictionary *result variable comes from. To me it seems it should be return type of addItem:item

DevilInDisguise
  • 360
  • 1
  • 4
  • 14
  • Not sure if I understand ... you first insert the item then execute the block with the result as a in parameter. Not weird at all :) – Peter Segerblom Mar 26 '15 at 15:13
  • It's not weird to do if you're planning on using `result` in some way within your block code. For example, you may want to check that your item was inserted correctly. – timgcarlson Mar 26 '15 at 15:16
  • @PeterSegerblom well, that sounds more like you are executing a method and then using the return as a parameter of another method. Not one method where the second parameter is result of the first being inserted. – DevilInDisguise Mar 26 '15 at 15:43
  • @timgcarlson yes, thats obv what result would be used for, but I don't understand where the result variable erupts from. it seems like being the execution of the method add item with just the first parameter. – DevilInDisguise Mar 26 '15 at 15:44

1 Answers1

1

Here is an explanation of how the above blocks work and in what order.

Completions aren't really return values, perhaps that is what is confusing you. A completion block is a way for you to send code to a method that can be run within the scope of that method.

So when you send a block to a method, this is a possible order of events...

- (void)addItem:(NSDictionary *)item completion:(CompletionBlock)completion {
    NSLog(@"1");
    [self.syncTable insert:item 
                completion:^(NSDictionary *result, NSError *error) {
                    NSLog(@"2");
                }
    ];
    NSLog(@"3"); 
}

// SyncTable.m?
- (void)insert:(NSDictionary *)item completion:(CompletionBlock)completion {
    NSLog(@"4");
    NSDictionary *result = ...;  // Prepare the result dictionary to be used in the completion block.
    NSError *error = nil
    completion(result, &error);
}

The order of these logs will output is as follows...

1
3
4
2

Notice that 2 is not logged until the completion block is called in insert:completion:? Does this clarify the use of parameters in a block?

timgcarlson
  • 3,017
  • 25
  • 52
  • well that is definitely cutting it out in baby pieces^^I was trying to locate the implementation of the insert function but could only find the header. It does clarify the subject for me, in the sense that I now can see the block is called after assignment of in this case the result and error variables within the method thats calling the block...... Been wrapping my head about too much stuff today. Thx a lot for your indeed very overly wellexplained answer. but how could u know i actually have a brain:) thx again, very much appreciated! – DevilInDisguise Mar 26 '15 at 21:41