iOS启动后台线程 [英] iOS start Background Thread

查看:108
本文介绍了iOS启动后台线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iOS设备上有一个小的sqlitedb。当用户按下按钮时,我从sqlite&获取数据。向用户显示。

I have a small sqlitedb in my iOS device. When a user presses a button, I fetch the data from sqlite & show it to user.

这个抓取部分我想在后台线程中执行它(不阻止UI主线程)。我是这样做的 -

This fetching part I want to do it in a background thread (to not block the UI main thread). I do this like so -

[self performSelectorInBackground:@selector(getResultSetFromDB :) withObject:docids];

提取后&一点点处理,我需要更新UI。但是因为(作为一种好的做法)我们不应该从后台线程执行UI更新。我在mainthread上调用选择器,如此 -

After the fetching & a little bit of processing, I need to update the UI. But since (as a good practice) we should not perform UI updation from background threads. I call a selector on mainthread like so -

[self performSelectorOnMainThread:@selector( showResults)withObject:nil waitUntilDone:NO];

但我的应用程序在第一步崩溃了。即启动后台线程。这不是在iOS中启动后台线程的方法吗?

But my App crashes in the first step. i.e. starting a background thread. Is this not a way to start background threads in iOS?

UPDATE 1: 之后[self performSelectorInBackground .... 我得到这个堆栈跟踪,没有信息是什么 -

UPDATE 1: After [self performSelectorInBackground.... I get this stacktrace, no info what so ever -

更新2:我甚至试过,开始这样的后台线程 -
[NSThread detachNewThreadSelector:@selector(getResultSetFromDB :) toTarget:self withObject:docids]; 但我仍然得到相同的堆栈跟踪。

UPDATE 2: I even tried, starting a background thread like so - [NSThread detachNewThreadSelector:@selector(getResultSetFromDB:) toTarget:self withObject:docids]; but still I get same stacktrace.

这样我就澄清了,当我在主线程上执行此操作时,一切运行顺利...

Just so that I clarify, when I perform this operation on main thread everything runs smooth...

更新3 这是我尝试从后台运行的方法

UPDATE 3 This is the method I am trying to run from background

- (void)getResultSetFromDB:(NSMutableArray *)toProceessDocids
{
    SpotMain *mirror = [[SpotMain alloc] init];
    NSMutableArray *filteredDocids = toProceessDocids;

    if(![gMediaBucket isEqualToString:@""])
        filteredDocids = [mirror FetchDocIdsForMediaBucketWithDocID:filteredDocids mBucket:gMediaBucket numRes:-1];
    if(![gMediaType isEqualToString:@""])
        filteredDocids = [mirror FetchDocIdsForMediaType:filteredDocids mediaType:gMediaType numRes:-1];
    if(![gPlatform isEqualToString:@""])
        filteredDocids = [mirror FetchDocIdsForPlatformID:filteredDocids platformId:@"1" numRes:-1];

    self.resultSet = [mirror FetchObjectFromDocid:filteredDocids];
    [filteredDocids release];
    [mirror release];

    [self performSelectorOnMainThread:@selector(showResults) withObject:nil waitUntilDone:NO];
    return;
}


推荐答案

如果您使用 performSelectorInBackground:withObject:要生成一个新线程,然后执行的选择器负责设置新线程的自动释放池,运行循环和其他配置细节 - 请参阅使用NSObject在Apple的线程编程指南中生成线程

If you use performSelectorInBackground:withObject: to spawn a new thread, then the performed selector is responsible for setting up the new thread's autorelease pool, run loop and other configuration details – see "Using NSObject to Spawn a Thread" in Apple's Threading Programming Guide.

使用 Grand Central Dispatch ,但是:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self getResultSetFromDB:docids];
});

GCD是一种较新的技术,在内存开销和代码行方面效率更高。

GCD is a newer technology, and is more efficient in terms of memory overhead and lines of code.

已更新,提示 Chris Nolet ,他提出了一个更改,使上述代码更简单,并与Apple最新的GCD代码示例保持一致。

Updated with a hat tip to Chris Nolet, who suggested a change that makes the above code simpler and keeps up with Apple's latest GCD code examples.

这篇关于iOS启动后台线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆