I have an method (let's call it method1
) that makes an asynchronous request to a server (POST) to create a CommunicationLink object with a given ID. Before sending the request, the method checks if the CommunicationLink already exists on the server.
I have a second method (let's call it method2) that fetches a list of "Topic" objects associated with a given CommunicationLink ID. Just like method1
, method2
checks if the CommunicationLink already exists on the server, and if not, it creates it (again, by sending a asynchronous request to the server).
The problem I have is that when I start my application, method1
and method2
are called one after the other, so I end up with something like this:
method1 checks if CommunicationLink exists ---> NO
method1 sends a request to create CommunicationLink (async call)
method2 checks if CommunicationLink exist ---> NO because the previous call is not done yet
method2 sends a request to create CommunicationLink
method1 done
method2 done
So on the server, I end up with 2 CommunicationLinks for the same ID (I don't have control of what's happening on the server side)
What would be the best way to "pause" method2
if method1
already sent a request to the server? I can set a boolean to true
before the call in method1
and set it back to false
once the request is done and have a while (!myValue);
in method2
, but I have a feeling that there's a better way to do this.
Edit
This code if part of a static library I'm working on to deal with push notifications. I"m also working on a test app that uses the library. method1
is called in - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken;
and method2
is called in my root view's viewDidLoad
Method1
- (void)sendProviderDeviceToken:(NSData *)deviceToken contextUUID:(NSString *)contextUUID sourceUUID:(NSString *)sourceUUID topicUUID:(NSString *)topicUUID language:(NSString *)language;
Method that creates CommLink
- (void)createCommunicationLinkWithSuccessHandler:(void (^)(NSNumber *))successHandler errorHandler:(void (^)(NSError *))errorHandler;
Method2
- (void)retrieveSubscriptionForContext:(NSString *)contextUUID notificationTopic:(NSString *)notificationTopic completion:(void (^)(NSDictionary *))completion;
I would like to be able to handle this case directly in my library, and not have to modify my test app. method1
and method2
are called on the same thread.
Thanks