3

I want to show users current location in map and I know it's not instantaneous task. I want to call my showCurrentLocation() function in the ASYNC task. I was trying to learn call back closures but i could not understand how can I create ASYNC task for this. I know this is not a best question template for stackoverflow but I don't know how can I ask differently. Thank you for any help. Have a nice coding.

2
  • 1
    have a look at NSOperation nshipster.com/nsoperation Commented Aug 24, 2015 at 11:22
  • Thank you. I will look the NSOperation Commented Aug 24, 2015 at 11:30

2 Answers 2

5

In the past I've made a class called AsyncTask for usage purposes like the one you described here.

Lately I've updated it for swift 3

It has 2 generic types:

BGParam - the type of the parameter sent to the task upon execution.

BGResult - the type of the result of the background computation.

If one of them(or both) isn't needed you can set it to Optional or just ignore.

In code example I refer the functions showCurrentLocation() & getCurrentLocation() that you mentioned in OP & comments. BGParam is set to Int & BGResults is set to CLLocationCoordinate2D

AsyncTask(backgroundTask: {(param:Int)->CLLocationCoordinate2D in
        print(param);//prints the Int value passed from .execute(1)
        return self.getCurrentLocation();//the function you mentioned in comment
        }, afterTask: {(coords)in
            //use latitude & longitude at UI-Main thread
            print("latitude: \(coords.latitude)");
            print("latitude: \(coords.longitude)");
            self.showCurrentLocation(coords);//function you mentioned in OP
    }).execute(1);//pass Int value 1 to backgroundTask
Sign up to request clarification or add additional context in comments.

Comments

4

There is a technology called GCD (Grand Central Dispatch) which you can use to perform such tasks.

let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
    // do some task
    dispatch_async(dispatch_get_main_queue()) {
        // update some UI
    }
}

2 Comments

First of all thank you for your answer. I know the GCD. I wonder, Can I create custom async task with closure ? Think about this situation. Lets assume we have a getCurrentLocation() function and it returns us userCurrentLocation.latitude and longitude. I can't get values instantaneously so I have to use callback mechanism. It should tell me we found locations and there are the results so I can use them in main thread. If you put the getCurrentLocation function in the background thread. Does it solve my problem ? Will it work like a callback mechanism ? Thank you !
using the global queue is not the best idea ... you newer know who use it. creating own concurrent queue is a little bit better solution here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.