Merge results from NSURLSession back on to the main thread UI

I have apps that send requests for route between 2 locations and search, filter then display facilities near the route. The apps first send a request for the route on a background thread, then based on route, search for facilities near certain locations on or near the route. There maybe multiple searches on the same route, each on a different location. Suitable results then are displayed on the map. Apps also do live updates. However, since I have switched to using NSURLSession to search for the route on a background thread, not all suitable results/pin are displayed. Certain pins only show up upon the next didUpdateToLocation call. So my question is, what is the best practice to sync the results on the UI? Why do only some of the results show up on the UI, and others don't.

Answered by DTS Engineer in 798206022

The story here is different for Objective-C and Swift. In Swift we are rolling out Swift concurrency. Its goal is to give the compiler full knowledge about execution contexts, which helps it diagnose problems like this an compile time.

It sounds like you’re using Objective-C, in which case you don’t get that compiler support. You have to understand the rules and ensure that you follow them. That can be tricky.

However, since I have switched to using NSURLSession to search for the route on a background thread, not all suitable results/pin are displayed.

How did you create your session?

In general, I create my sessions like this:

@interface MyClass : NSObject <NSURLSessionTaskDelegate>

@property (nonatomic, strong, readonly) NSURLSession * session;

@end

@implementation MyClass

- (instancetype)init {
    self = [super init];
    if (self != nil) {
        NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration];
        … tweak `config` …
        self->_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    }
    return self;
}

@end

That runs the delegate callbacks on the main thread, so I can update my UI directly.

Why do only some of the results show up on the UI, and others don't.

It’s hard to say for user without digging much deeper into your code but, in general, concurrency bugs tend to have weird symptoms. One common symptom is for UI frameworks not to update correctly. It’s possible that’s what’s happening here, but it could be something else completely different. Regardless, you need to rule out the obvious concurrency problems before investigating further.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The story here is different for Objective-C and Swift. In Swift we are rolling out Swift concurrency. Its goal is to give the compiler full knowledge about execution contexts, which helps it diagnose problems like this an compile time.

It sounds like you’re using Objective-C, in which case you don’t get that compiler support. You have to understand the rules and ensure that you follow them. That can be tricky.

However, since I have switched to using NSURLSession to search for the route on a background thread, not all suitable results/pin are displayed.

How did you create your session?

In general, I create my sessions like this:

@interface MyClass : NSObject <NSURLSessionTaskDelegate>

@property (nonatomic, strong, readonly) NSURLSession * session;

@end

@implementation MyClass

- (instancetype)init {
    self = [super init];
    if (self != nil) {
        NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration];
        … tweak `config` …
        self->_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    }
    return self;
}

@end

That runs the delegate callbacks on the main thread, so I can update my UI directly.

Why do only some of the results show up on the UI, and others don't.

It’s hard to say for user without digging much deeper into your code but, in general, concurrency bugs tend to have weird symptoms. One common symptom is for UI frameworks not to update correctly. It’s possible that’s what’s happening here, but it could be something else completely different. Regardless, you need to rule out the obvious concurrency problems before investigating further.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Merge results from NSURLSession back on to the main thread UI
 
 
Q