iOS make task run on background thread Thread safe?

643 views Asked by At

In my app I'm calling a web service after every 3 seconds on background thread using dispath_async and save the response in coredata after that fetch records from coredatda and display on the UI.My code is

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{


        [[WebService webServicesManager]getDataWithPath:str ResponseBlock:^(NSString* response, NSString* error)
         {
             if (response != nil) {

                 // save records in core data
                     }
                     dispatch_async(dispatch_get_main_queue(), ^(void) {

                         //Code here is run on the main thread
                       [self showNewRecordsFromDB];


                     });
                 }
             }

        }];

    });

Code above run fine for some time, but after some time(~ 4-5 minutes) i'm getting same data multiple times i.e. same record is showing on screen multiple times.

Is is thread safety problem or any other mistake in my code? please assist me to overcome from this problem.

Edit - I replaced the concurrent queue with serial queue-

dispatch_queue_t = dispatch_queue_create("Messgae Queue",NULL);
dispatch_async(queue, ^{

 //rest code same as above

    });

and i'm getting no issue. Is it ok or i still have to use different context for different thread?

0

There are 0 answers