Ios 发送用户脱机时创建的数据

Ios 发送用户脱机时创建的数据,ios,swift,Ios,Swift,我想允许用户在脱机时创建一些项目。然后,当用户重新连接到internet时,将创建的项目发送到后端。我很困惑,正确的方法是什么 我是否应该使用URLSession的waitsforconnectivity,即使用户关闭应用程序,它也会发送请求 或者我应该安排一个后台任务?如果是,那么当用户连接到internet时,如何触发此任务 注意:我使用的是网络我想你把这个复杂化了 如果您使用Alamofire进行联网,那么我不建议使用第一种方法,因为这将混合使用URLSession和Alamofire

我想允许用户在脱机时创建一些项目。然后,当用户重新连接到internet时,将创建的项目发送到后端。
我很困惑,正确的方法是什么

  • 我是否应该使用URLSession的
    waitsforconnectivity
    ,即使用户关闭应用程序,它也会发送请求
  • 或者我应该安排一个后台任务?如果是,那么当用户连接到internet时,如何触发此任务

注意:我使用的是网络

我想你把这个复杂化了

如果您使用Alamofire进行联网,那么我不建议使用第一种方法,因为这将混合使用
URLSession
和Alamofire进行联网,这不是一个好主意

关于你的第二种方法。为什么它必须是后台任务?为什么你不能检查用户是否首先连接到互联网,如果是,你可以正常进行。如果没有,那么就创建项目并以某种方式缓存它们。然后,当您重新连接到internet时,您可以先发送缓存的项目,就像发送普通项目一样


Alamofire有一个内置软件,可以帮助您确定网络状态。中有一个很好的例子可以使用它。

您可以使用Alomafire本身来实现: 网络管理器

class NetworkManager {

//shared instance
static let shared = NetworkManager()

let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.google.com")

func startNetworkReachabilityObserver() {

    reachabilityManager?.listener = { status in
        switch status {

            case .notReachable:
                print("The network is not reachable")

            case .unknown :
                print("It is unknown whether the network is reachable")

            case .reachable(.ethernetOrWiFi):
                print("The network is reachable over the WiFi connection")

            case .reachable(.wwan):
                print("The network is reachable over the WWAN connection")

            }
        }

        // start listening
        reachabilityManager?.startListening()
   }
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // add network reachability observer on app start
        NetworkManager.shared.startNetworkReachabilityObserver()

        return true
    }
}
可达性观察者

class NetworkManager {

//shared instance
static let shared = NetworkManager()

let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.google.com")

func startNetworkReachabilityObserver() {

    reachabilityManager?.listener = { status in
        switch status {

            case .notReachable:
                print("The network is not reachable")

            case .unknown :
                print("It is unknown whether the network is reachable")

            case .reachable(.ethernetOrWiFi):
                print("The network is reachable over the WiFi connection")

            case .reachable(.wwan):
                print("The network is reachable over the WWAN connection")

            }
        }

        // start listening
        reachabilityManager?.startListening()
   }
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // add network reachability observer on app start
        NetworkManager.shared.startNetworkReachabilityObserver()

        return true
    }
}

感谢您的重播,我正在尝试将创建的数据安全地发送到服务器。我不想等待用户打开应用程序发送这些请求。