views:

35

answers:

1

I have a situation similar to this: http://stackoverflow.com/questions/2698591/objective-c-how-to-use-memory-managment-properly-for-asynchronous-methods

I have an object that asynchronously downloads & parses an xml doc. It then has a delegate method that transfers the data it retrieved to the caller.

My 2 questions are:

When do I release the data retrieving object? The link I posted above gives 2 answers, one says release in the delegate and one says release immediately, which is correct (or which is better if both answers are correct)

My second question is, what is the best way to transfer the retrieved data to the caller? At the moment I have

self.imagesDataSource = [articleImagesParserObject.returnedArray copy];

I used copy because, as far as I understand, that makes the mutable array immutable. Is that correct?

A: 

I'm going to pick you up on a couple of things.. It might start the ball rolling :)

You say

It then has a delegate method that transfers the data it retrieved to the caller

-- EDIT --
You mean that you send a message to the NSURLConnection's delegate. Yes, it is just semantics, but it is clearer.

You say

The link I posted above gives 2 answers, one says release in the delegate and one says release immediately

The post you link to says that if you launch your secondary thread with NSThread +detachNewThreadSelector:toTarget:withObject: the thread will retain your object, so if you have finished with it, you can release it, as is normal practise. You are not doing this.

The second suggested method is to supply a method to callback when your background operation has completed. As you are using NSURLConnection and it already provides you with callback hooks, and in fact you are using them to return your downloaded data, this seems like the way to go.

Copying a mutable array does give you an immutable copy, which you own - so it should be self.imagesDataSource = [[articleImagesParserObject.returnedArray copy] autorelease] unless imagesDataSource doesn't retain - which would be irregular.

mustISignUp
If you've never heard of delegate methods, you can't have read Apple's documentation. The delegation pattern is used all over the place, and the term "delegate method" is in the docs for every class that uses the pattern.
Chuck
i think you can have a delegate object, which necessarily has methods. But consider an NSTableView instance; i can't say 'i would like [obj1 hasBeans] to be the delegate method for textShouldBeginEditing:, i would like [obj78 isVisible] to be the delegate method for textShouldEndEditing:. No, i can set the delegate Object - but that is all. Furthermore the OP is conflating delegation and messaging.
mustISignUp
Conflating? Your abyssopelagic response is a pleonasm that obfuscates any information it might have contained ;-)Jokes aside thoughDelegate Method:http://en.wikipedia.org/wiki/Delegation_patternhttp://cocoadevcentral.com/articles/000075.php
Spider-Paddy
"However, you don't say how you start the secondary thread and you don't post any code."Line 3: I have an object that asynchronously downloads
Spider-Paddy
So, you are using NSURLConnection - sorry i missed the invisible bit of line 3 where you stated that. If i squint i can see it now. self.imagesDataSource = [articleImagesParserObject.returnedArray copy] is fine for your NSURLConnection delegate (apart from the leak). Can i suggest that you put NSURlConnection in the title instead of 'asynchronous NSObject' ?
mustISignUp