views:

81

answers:

2

I have the following method:

- (NSMutableArray *)getElements:(NSString *)theURL;

And I wanted to know if there is a way to call that method using performSelectorOnMainThread so that I can get the return value. So far, I've tried with:

myArray = [object performSelectorOnMainThread:@selector(getElements:)
                                   withObject:url waitUntilDone:YES];

but it doesn't work, since performSelectorOnMainThread returns void. How could I solve this?

+2  A: 

You can't do it directly, because, as you say, that method returns void.

So, you'd have to arrange another way to get a value back, for example by passing an NSDictionary instead of an NSString, and having the method store the result in the dictionary for you.

David Gelhar
+5  A: 

Welcome to a multi-threaded environment my friend.

You'll need to store the return value in an instance variable or pass in an object by reference through the withObject parameter:

NSMutableDictionary *myDict;

[object performSelectorOnMainThread:@selector(getElements:)
                                   withObject:myDict waitUntilDone:YES];

Your method prototype should now look like this:

- (void)getElements:(NSMutableDictionary *)objects;
Jacob Relkin