0

So I have a chunk of code that does some magic with a query and gives me a variable called JSONresult which has been told to run in it's own thread. Once I get this result, I would like take that variable from that thread and make it so that when an IBAction method is called on (triggerd by a button press) if data in the variable JSONresult exists that it will display it in an NSTextView.

So far I've been able to get the data to appear in an NSTextView if I specify the data manually within the IBAction method, but trying to use the JSONresult variable doesn't work for obvious reasons.

Here is what I've got so far.

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [NSThread detachNewThreadSelector:@selector(getJSONquery) toTarget:self withObject:nil];
}

-(void)getJSONquery {
    [[NSThread currentThread] setName:@"Get JSON Query"];

    //Some fancy stuff here

    NSArray *JSONresult = [string componentsSeparatedByString:@"<|...|>"];
}

After all this has happened and I've now got my JSONresult I have an IBAction setup so that if the button is pushed, the JSONresult will be taken and displayed in a NSTextView

-(IBAction)showContent:(id)sender {
    [content setString:JSONresult];
}

How would I go about passing the variable JSONresult between the two threads/methods?

Thanks in advance! :)

Also if I should update this post with the header file please let me know. I'm new to the world of Objective C and have a very bad understanding of the general how-to so I don't know whether it's relevant to this situation or not

SteppingHat
  • 1,199
  • 4
  • 19
  • 50
  • possible duplicate of [Passing Data Between Threads](http://stackoverflow.com/questions/4646264/passing-data-between-threads) – JensG May 12 '14 at 11:50

2 Answers2

1

You don't really pass data between threads, as all threads have access to the same data within a process, however you need synchronize access to the data so that multiple threads don't trample over the shared data, which can lead to its corruption.

In your case, however I don't think their is much synchronization required so your question isn't about passing data between threads, it's more to do with how to make the JSON result available to the button method.

As has already been answered by @Leandros, you would normally do this using a private property:

YourClass.m:

@interface YourClass ()
@property NSArray *jsonResult;     // Note: strong, atomic
@end

...

-(void)getJSONquery {
    [[NSThread currentThread] setName:@"Get JSON Query"];

    //Some fancy stuff here

    self.jsonResult = [string componentsSeparatedByString:@"<|...|>"];
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Why do you use a atomic property? Does it really have to be thread safe? – Leandros May 12 '14 at 10:20
  • @Leandros Yes, as multiple threads are using the property, however given the setter/getter is merely assigning a pointer it won't make much difference at all. – trojanfoe May 12 '14 at 10:22
  • Sure, but since I can check if the pointer is set and only one thread is modifying it, I don't think it matters. But I agree, it isn't bad either. – Leandros May 12 '14 at 10:26
0

You can create a property.

// You can add this to the .h file.
@property (atomic, strong) NSArray *JSONresult;

// Or this above the @implementation in your .m file.
@interface YourClass()
@property (atomic, strong) NSArray *JSONresult;
@end

- (void)getJSONquery {
    [[NSThread currentThread] setName:@"Get JSON Query"];

    //Some fancy stuff here

    self.JSONresult = [string componentsSeparatedByString:@"<|...|>"];
}

- (IBAction)showContent:(id)sender {
    if (self.JSONresult) {
        [content setString:self.JSONresult];
    }
}
Leandros
  • 16,805
  • 9
  • 69
  • 108