pic-o-matic
pic-o-matic

Reputation: 241

How to "talk" back from object B to A?

i have a view controller object A, that asks another object B for internet data. Object B implements: connectionDidFinishLoading:connection:

When object B is ready and has its data, the method above is called. But i want to update the UI of object A. Whats the best way to do this? I think i must use a protocol?

Upvotes: 0

Views: 66

Answers (1)

Duncan C
Duncan C

Reputation: 131408

There are different ways to handle this. Setting up object A to be the delegate of object B would be a perfectly valid way. you'd then define a protocol that object B would use to communicate back to Object A.

Another way to pass a completion block from object A to object B in the call that asks for the data. Then in object B's connectionDidFinishLoading:connection: method, it would check if the completion block is nil and if it's not nil, call the block.

I have been using the block-based approach more and more lately. There is less setup, and the completion code is right in the method in Object A that invokes Object B, so it's easier to follow what's going on.

You might want to set up a block that takes a success/failure parameter, as well as an NSError object. That way if the connection fails, you can handle it gracefully, and even report the error to the user if you decide to.

Upvotes: 2

Related Questions