-1

Sorry about the confusing title, I have this block of code running in its own thread and want to access a class variable inside another class (view controller) everytime its value changed.

To clarify my question here is a simple class structure that represent what I’m trying to accomplish.

@interface classB : NSObject 
{
  NSThread * _someThread;
}

+ (classB*) instance;

@property(atomic) CVPixelBufferRef pixBufB;

- (void) foo
{
  while(1) 
  {

  //decode a frame

  //assign data to pixBufB

  }
}

- (void) start
{
  _someThread = [[NSThread alloc] initWithTarget:self selector:@selector(foo) object:nil];
}

//sampleViewController 

@interface sampleViewController : UIViewController

@property(atomic) CVPixelBufferRef pixBuf;

- (void)viewDidLoad
{
        [[classB instance] start];
}

- (void) bar 
{
    _pixBuf =  [[classB instance] pixBufB];
}

At the end of each loop cycle in foo, I want to access _pixBufB inside sampleViewController class. Since foo is executed in another thread I can’t simply use a getter, does anyone know how to approach this issue?

Thanks

Daniyar
  • 2,975
  • 2
  • 26
  • 39
C4DZ
  • 9
  • 2

2 Answers2

0

You can do this work by using NSOperation and NSOperationQueue. You can "task" an assignment to a subclass of NSOperation and overwrite the main method to write a task. Next you need to make an object of NSOperationQueue and add your subclass, and it will start running in the new Queue u created and you can create it as synchronous and asynchronous respectively.

Now you can add a onCompletion Block at the end of the Queue OR the NSOperation itself, and it will be performed. OR you can create another subclass of NSOperation and create your task that you want to perform (foo, here) as another task and addDependency with the First TASK, therefore the second task(foo) will be performed only after the first is finished.

For more about NSOperation and NSOperationQueue, you can visit this link

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
0

Have the thread take a block to be called at each point you want to do an update. The block can either be dispatched to the main queue to be called, or it can itself dispatch to the main queue before updating any view controller state.

Avi
  • 7,469
  • 2
  • 21
  • 22