0

I've come to a problem where proper threading is needed, but I can't seem to optimise it correctly.

Here's my method:

-(void) method1
{
  // -1 to an NSInteger
  nsint1--;

  [self showActiviyIndicator:YES]; //act as loading screen

  [alloc database etc stuffs and retrieving of data here]

  //for loop here to check with database, and grey out button depending on database values
  for (int i = 1; i<12; i ++)
  {
  //get values from database and store into variables, then grey out the button if variables are 0.
  }

  int Val1 = [get from database]

  if Val1 = 0
  [button setTitleColor:[UIColor Grey]];

  someLabel.text = [NSString stringWithFormat:@"%ld", (long)nsint1];

  //here's where the problem lies
  [self refreshTableSessionList:xx];

  [self showActiviyIndicator:NO]
}

inside [self refreshTableSessionList:xx], there's a

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)

to get data from server database, then a

dispatch_async(dispatch_get_main_queue(),

to populate and reload tableViewCell.

But there'll be a conflict for when I put a

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)

before [alloc database etc stuffs and retrieving of data here] and put dispatch_async(dispatch_get_main_queue(), when greying out the button, but that's inside a loop, which i don't think it is the right way.

What's the solution to overcome this?

JackyW
  • 345
  • 2
  • 11
  • Use block to get data from server database. – Kampai Mar 17 '17 at 07:28
  • Where is your { in your for loop? –  Mar 17 '17 at 07:37
  • Possible duplicate of [iPhone - Grand Central Dispatch main thread](http://stackoverflow.com/questions/7905192/iphone-grand-central-dispatch-main-thread) –  Mar 17 '17 at 07:39
  • @Kampai , will look into it. – JackyW Mar 17 '17 at 07:42
  • @Sneak , I didn't list it, but the comments show what the loop is about, will edit it. And will look into the link you gave. thank you. – JackyW Mar 17 '17 at 07:42
  • No worries. Try to provide easy to read code in your questions. However, check the multiple threads made on this topic on how you should handle threads. You can give this google link a go for more threads to read: https://www.google.se/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=dispatch+async+ios+stackoverflow&* Also, remember, when you run background stuff in your for loop, and you do the other stuff on a different thread (main) , they don't wait for eachother to finish, thats probably why you get unwanted results. (inside your refreshTableSessionList:xx calls that is) –  Mar 17 '17 at 07:44

1 Answers1

1

As I understood you don't wait for the finish of the background database stuff.

Have you read about multithreading? For example, Ray's article.

In a simple way, you can call dispatch_async inside the dipatch_async block inside the dispatch_async and etc.

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  // do some database stuff
  dispatch_async(dispatch_get_main_queue(), ^{
    // do some UI stuff
  });
});

So you should switch between the main thread and a global queue. Also, you can use delegates, notifications or even reactivity for such purposes.

Vladimir Vlasov
  • 1,860
  • 3
  • 25
  • 38
  • It's great you want to help, but Please avoid answering questions with answers that have been answered like 100 times before. Flag it as a duplicate if you see fit. –  Mar 17 '17 at 07:40
  • Thanks for the link, looking at it now, as well as Ray's blocks tutorial. Thank you. – JackyW Mar 17 '17 at 07:43