简体   繁体   中英

iOS- How to create offline operation queue and execute when comes online?

I have just entry in iOS. I have a scenario : If user's device is offline then add his request (Web services) in a queue with his all request details parameters , and whenever his devices comes online , then hit web services (with data which is already stored) one by one/concurrently until queue empty. I have to use Core data. I am no any idea about this. So, I will be thankful if anyone share link (github/others any site) for sample project. Thanks in advance.

脱机时将数据存储在temp数据库中,联机时检查temp数据库是否有数据,然后使用递归或for循环将其逐一发送到服务器。在成功提交(在调用服务方法的完成处理程序中)后,删除该数据如果使用递归,请从temp数据库中调用并再次从中调用函数。

We had similar issues on our internal projects when connectivity spotty, so we wrote this framework that wraps any network request and allows it to be enqueued regardless of connectivity - https://cocoapods.org/pods/OfflineRequestManager . Anybody doing something similar might find it useful or at least draw inspiration from it. You'll still have to manage the network request itself, but it provides a fairly simple interface for ensuring that the app keeps trying to execute it until it succeeds, including saving to disk so it can be reattempted on startup.

The simplest use case would look something like the following, though most actual cases (saving to disk, specific request data, etc.) will have a few more hoops to jump through:

import OfflineRequestManager

class SimpleRequest: OfflineRequest {
    func perform(completion: @escaping (Error?) -> Void) {
        doMyNetworkRequest(withCompletion: { response, error in
            handleResponse(response)
            completion(error)
        })
    }
}
///////
OfflineRequestManager.defaultManager(queueRequest: SimpleRequest())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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