我知道如果我创建一个NSURLConnection(标准的异步),它将在同一个线程上回调.目前这是我的主线程.(工作也很好).
但是我现在使用相同的代码来做其他事情,我需要让我的UI保持活泼......
如果我做
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
/* and inside here, at some NSURLConnection is created */
});
Run Code Online (Sandbox Code Playgroud)
..我的NSURLConnection是否可能被创建,但我的线程在url连接返回之前消失了?
我是GCD的新手.在我的url连接返回之前,如何让线程保持活动状态,或者有更好的方法可以做到这一点?
我想同时下载一些文件,例如100个文件.所以我决定将我的下载线程添加到调度队列,GCD将调整同时运行的线程数.
这里的问题是:块dispatch_async将立即完成,因为task将在另一个线程上运行.因此,如果urls长度为100,它将立即创建100个线程.
var queueDownloadTask = dispatch_queue_create("downloadQueue", nil)
for url in urls {
dispatch_async(queueDownloadTask) {
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let fileTransferSession = NSURLSession(configuration: config)
let task = fileTransferSession.downloadTaskWithURL(url, completionHandler: { (responseUrl, response, error) -> Void in
println("completed")
})
task.resume()
}
}
Run Code Online (Sandbox Code Playgroud)
如何配置块dispatch_async以等待下载任务完成?我不想使用dispatch_semaphore,因为它只允许同时运行一个下载任务.
我无法弄清楚如何在Swift 3.0中重复调度计时器.我的代码:
let queue = DispatchQueue(label: "com.firm.app.timer",
attributes: DispatchQueue.Attributes.concurrent)
let timer = DispatchSource.makeTimerSource(flags: DispatchSource.TimerFlags(rawValue: UInt(0)),
queue: queue)
timer.scheduleRepeating(deadline: DispatchTime.now(),
interval: .seconds(5),
leeway: .seconds(1)
)
timer.setEventHandler(handler: {
//a bunch of code here
})
timer.resume()
Run Code Online (Sandbox Code Playgroud)
计时器只会触发一次并且不会像它应该的那样重复.我怎样才能解决这个问题?
当你需要在网络任务或操作的完成块中的主线程上执行某些操作时,哪种方式获取它将是最合适的,为什么?:
OperationQueue.main.addOperationDispatchQueue.main.async我有一个需要将数据(使用POST)发送到服务器的应用程序.此功能必须位于其中一个NavigationController子控制器上,用户应能够远离此控制器和/或关闭应用程序(仅支持iPhone4/iOS4).我应该使用线程/ NSOperations或/并使用现有的异步方法发送数据吗?任何想法/最佳实践如何实现这一点?
iphone multithreading nsoperation grand-central-dispatch ios
我在我的申请中使用Grand Central Dispatch(GCD)来做一些繁重的工作.该应用程序使用Core-Data进行数据存储.这是我的场景(以及相关问题):
dispatch_queue_t main_queue = dispatch_get_main_queue();
dispatch_queue_t request_queue = dispatch_queue_create("com.app.request", NULL);
dispatch_async(request_queue, ^{
MyNSManagedObject *mObject = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
// …
// <heavy lifting>
// …
// …
// <update mObject>
// …
[self saveManagedObjectContext];
});
Run Code Online (Sandbox Code Playgroud)
因此[self saveManagedObjectContext],fetchResultsController委托方法会自动调用.因此,UI更新逻辑启动.
现在的问题是,我需要用main_queue的-saveManagedObjectContext?我应该在我执行的所有操作NSManagedObject的main_queue?某些更新操作NSManagedObject可能需要2-3秒.请指教.
是否可以在主线程上运行完成块?
例如,我有一个返回值的方法:
- (int)test
{
/* here one method is called with completion block with return type void */
[obj somemethodwithcompeltionblock:
{
/* here I am getting my Int which I want to return */
}
];
}
Run Code Online (Sandbox Code Playgroud)
但我无法看到如何从完成块中返回整数值作为此方法的结果,因为完成块在后台线程上运行.
我怎样才能做到这一点?
什么是iOS中多线程的最佳方式,因为我们有三个选项GCD NSThread,和NSOperationQueue?我很困惑哪一个是最好的?如果没有,那么哪个应该用于什么情况以及它们如何不同,如果有人有一些很好的使用示例NSOperationQueue,请分享以便我可以学习.
multithreading nsthread nsoperationqueue grand-central-dispatch ios
在我的应用程序的许多地方,我使用下一个代码来执行后台任务并通知主线程:
dispatch_queue_t backgroundQueue = dispatch_queue_create("dispatch_queue_#1", 0);
dispatch_async(backgroundQueue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
});
});
Run Code Online (Sandbox Code Playgroud)
是否可以在一个地方创建一个backgroundQueue(哪种方式最好?)并在以后使用它?我知道系统全局队列,但订购对我来说很重要.
我正在努力了解DispatchSourceTimer,Timer和asyncAfter之间的主要区别(在我的情况下,该任务需要每X秒运行一次,尽管了解计时器的区别可能对有用)(或者是否有另一个(更有效)中除了列出的计时器外,还可以在Swift中使用调度机制?)。
A Timer需要在其开始的当前队列上有一个活动的运行循环。一个DispatchSourceTimer不需要。A Timer可防止CPU进入空闲状态。这是否也适用于DispatchSourceTimer/ asyncAfter?
在什么情况下,a Timer优于DispatchSourceTimer/ asyncAfter?当然,它们之间的区别是什么?
我想在私人队列中的应用程序中每15秒安排一次工作。这意味着我必须使用,DispatchSourceTimer因为我在不是主线程的队列中(或将runloop添加到队列中并使用Timer)。但是,即使Timer最初使用a,我也看不到任何好处。也许我可以使用另一个操作在私有队列上每X秒执行一次调度工作,该工作比a更为有效DispatchSourceTimer,但是我没有遇到更好的解决方案。
是一个DispatchSourceTimer比一个更有效Timer?还是应该继续使用的自调用方法asyncAfter?
这是创建计时器的代码。
异步之后
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(2)) {
// Code
}
Run Code Online (Sandbox Code Playgroud)
计时器
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { (_) in
// Code
}
Run Code Online (Sandbox Code Playgroud)
DispatchSourceTimer
let timer = DispatchSource.makeTimerSource()
timer.schedule(deadline: .now() + .seconds(1))
timer.setEventHandler {
// Code
}
timer.activate()
Run Code Online (Sandbox Code Playgroud)
所有计时器的利弊是什么?什么时候应该在另一个之上使用?哪种计时器方式最有效?我想出了以下几点: …