Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone - performSelectorOnMainThread with return value

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?

like image 931
The WebMacheter Avatar asked Jul 27 '10 17:07

The WebMacheter


2 Answers

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;
like image 91
Jacob Relkin Avatar answered Sep 28 '22 05:09

Jacob Relkin


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.

like image 29
David Gelhar Avatar answered Sep 28 '22 05:09

David Gelhar