我使用dispatch_group
编写了一个方法:
class func loadNewEvents(inContext context:NSManagedObjectContext, completion:(() -> Void)?=nil) {
DDLogDebug("Loading New Events")
let context = CoreDataManager.backgroundContext()
let events = CoreDataManager.fetch(Event.self, inContext: context)
var earliestEvent = events.sort({$0.initialTimestamp.integerValue > $1.initialTimestamp.integerValue}).first
let group = dispatch_group_create()
var loadedEvents:[Event]?
var failure = false
while (loadedEvents == nil || loadedEvents!.count != 0) && !failure {
dispatch_group_enter(group);
if let earliestTimeStamp = earliestEvent?.initialTimestamp.longLongValue {
let url = afterUrl(earliestTimeStamp)
getEvents(url: url,
success: { events in
loadedEvents = events
earliestEvent = events.last
dispatch_group_leave(group)
},
failure: {
failure = true
dispatch_group_leave(group)
}
)
} else {
break
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER)
}
DDLogDebug("Loaded new events")
}
它在iOS9模拟器和带有iOS9的iPhone5S上运行良好。 但是在带有iOS8的iPhone 4S上,方法的最后会发生崩溃:
EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xdefe)
中的 _dispatch_semaphore_dispose
知道会导致这种情况的原因以及如何解决这个问题?
答案 0 :(得分:1)
我终于找到了问题:我没有让小组离开break
。
要解决此问题,我在dispatch_group_enter(group)
内拨打了if let
,因为当我没有进行异步通话时不需要它。