【发布时间】:2015-12-15 14:00:21
【问题描述】:
我需要在我的应用启动时设置一些初始数据。我有一个名为loadData() 的方法,它在其中进行了多次调用,需要互联网连接。我的代码如下所示:
func setup() {
if reachability.isReachable() {
// If there is connection, we setup the initial data
loadData()
} else {
//If there is no connection, wait for a notification
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: nil)
do {
try reachability.startNotifier()
} catch {
print("Unable to start notifier")
}
}
}
func loadData() {
loadA()
loadB()
loadC()
..
loadZ()
}
func reachabilityChanged(note: NSNotification) {
if reachability!.isReachable() {
loadData()
reachability!.stopNotifier()
}
}
这让我可以处理在启动loadData() 之前没有连接的情况。但是,如果连接丢失,例如在loadC() 期间,我将无法添加通知以便稍后加载丢失的数据。我怎样才能做到这一点?
【问题讨论】:
-
你想怎么实现呢?如何将所有项目添加到队列中,以便暂停它并添加它们之间的依赖关系?
-
@Wain 这样的事情似乎是对的,我该怎么做?
-
看上次wwdc的nsoperation谈话
标签: ios swift reachability