简体   繁体   English

如何启动和停止后台线程iOS

[英]How to start and stop background thread iOS

I am developing an iPhone app in which, I want start background thread for downloading image from server. 我正在开发一个iPhone应用程序,其中,我要启动后台线程以从服务器下载图像。 while downloading the image if user wants to cancel a download then on click of button he can cancel downloading means I want to stop execution of background thread. 在下载图像时,如果用户要取消下载,则单击按钮时他可以取消下载,这意味着我想停止执行后台线程。

is it possible to do ? 有可能吗? in iPhone 在iPhone中

you can start a continue the background download with this code. 您可以使用此代码开始继续进行后台下载。

taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
    }];

//code to download .

[[UIApplication sharedApplication] endBackgroundTask:taskId];

you can call 你可以打电话

[[UIApplication sharedApplication] endBackgroundTask:taskId];

to stop the background task. 停止后台任务。

But this download will continue only for few minutes . 但是此下载将仅持续几分钟。 Then app will crash if it continue downloading. 然后,如果继续下载,该应用将崩溃。

If you want to perform an operation in the background, then you'll need to wrap it in a background task. 如果要在后台执行操作,则需要将其包装在后台任务中。 It's also very important that you call endBackgroundTask when you're finished - otherwise the app will be killed after its allotted time has expired. 完成后调用endBackgroundTask也很重要-否则应用程序将在其分配的时间到期后被终止。

Something like this should work for you: 这样的事情应该为您工作:

Declare a property: 声明财产:

@property(assign, nonatomic) UIBackgroundTaskIdentifier backgroundFetchTask;

Then: 然后:

- (void) fetchImageInBackground
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    [self beginBackgroundFetchTask];

    NSURLResponse * response = nil;
    NSError  * error = nil;
    NSData * responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];

        // Store your image

        [self endBackgroundFetchTask];
    });
}

- (void) beginBackgroundFetchTask
{
    self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [self endBackgroundFetchTask];
    }];
}

- (void) endBackgroundFetchTask
{
    [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
    self.backgroundFetchTask = UIBackgroundTaskInvalid;
}

You can use AFNetworking for this purpose. 您可以将AFNetworking用于此目的。 https://github.com/AFNetworking/AFNetworking https://github.com/AFNetworking/AFNetworking

AFImageRequestOperation exactly does this. AFImageRequestOperation正是这样做的。

Example to download image: 下载图片的示例:

AFImageRequestOperation * _operation = [AFImageRequestOperation imageRequestOperationWithRequest:request success:^(UIImage *image) {
    // image is your image
    _operation = nil;
}];

[_operation start];

Example to cancel thread: 取消线程的示例:

[_operation cancel];

So in your case -> declare variable AFImageRequestOperation *_operation; 因此,在您的情况下->声明变量AFImageRequestOperation * _operation; in the @interface or @implementation and use it to download your images in background. 在@interface或@implementation中,并使用它在后台下载图像。

I would do a singlton class MyImageBackgrounfDownloader 我会做一个singlton类MyImageBackgrounfDownloader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM