本文介绍了在Swift 2中创建ASYNC任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向用户显示地图中的当前位置,我知道这不是即时任务。我想在ASYNC任务中调用我的showCurrentLocation()函数。我正在尝试学习回调闭包,但是我不明白如何为此创建ASYNC任务。我知道这不是Stackoverflow的最佳问题模板,但我不知道该如何提出不同的要求。
谢谢您的帮助。
编码不错。

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.

推荐答案

过去,我制作了一个名为出于使用目的,如您在此描述的那样。

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

最近我已经已将其更新为 swift 3

Lately I've updated it for swift 3

它具有2种通用类型:

BGParam -执行时发送给任务的参数类型。

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

BGResult -后台计算结果的类型。

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

如果不需要其中一个(或两个),则可以将其设置为Optional或忽略。

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

在代码示例中,我引用函数 showCurrentLocation()&您在OP&中提到的 getCurrentLocation()评论。
BGParam 设置为 Int & BGResults 设置为 CLLocationCoordinate2D

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

这篇关于在Swift 2中创建ASYNC任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:40