我有一个iOS应用程序,某些用户会崩溃。我无法重新创建崩溃,但是我确实有三个完全相同的崩溃报告。我如何找出导致崩溃的原因? (我看过https://developer.apple.com/videos/play/wwdc2018/414/,但仍然遇到困难。)
崩溃报告中的摘录如下:
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x0000000100fd21ac
Termination Signal: Trace/BPT trap: 5
Termination Reason: Namespace SIGNAL, Code 0x5
Terminating Process: exc handler [5535]
Triggered by Thread: 0
Thread 0 name:
Thread 0 Crashed:
0 libswiftCore.dylib 0x0000000100fd21ac 0x100de0000 + 2040236
1 libswiftCore.dylib 0x0000000100fd21ac 0x100de0000 + 2040236
2 libswiftCore.dylib 0x0000000101027c50 0x100de0000 + 2391120
3 MyApp 0x000000010081af08 partial apply for closure #3 in DefaultService.processPosts(network:posts:completion:) + 40 (DefaultService.swift:1009)
4 MyApp 0x000000010087060c thunk for @escaping @callee_guaranteed () -> () + 36 (<compiler-generated>:0)
5 libdispatch.dylib 0x000000021aeb96c8 _dispatch_call_block_and_release + 24 (init.c:1372)
6 libdispatch.dylib 0x000000021aeba484 _dispatch_client_callout + 16 (object.m:511)
似乎在DefaultService.swift:1009
中引起问题processPosts
的代码如下:
private func processPosts(network: Network, posts: [Post], completion: @escaping (Result<Void>) -> Void) {
...
dg.notify(queue: .main) {
self.savePosts(network: network, posts: posts) // This is line 1009.
completion(.success(()))
}
}
private func savePosts(network: Network, posts: [Post]) {
let context = self.container().viewContext
for post in posts {
// Don't save if already saved.
if isPostSaved(context: context, network: network,
networkPostID: post.networkPostID!) {
continue
}
let np = NetworkPost(context: context)
np.id = post.id
np.network = network.rawValue
np.networkPostID = post.networkPostID
np.subtitle = post.subtitle
np.text = post.text
for image in post.images {
let npImage = NetworkPostImage(context: context)
npImage.data = image
np.addToImages(npImage)
}
if let active = post.active {
np.active = active
}
np.created = post.created
}
try! context.save()
}
private func isPostSaved(context: NSManagedObjectContext, network: Network, networkPostID: String) -> Bool {
let fetchRequest = NSFetchRequest<NetworkPost>(entityName: "NetworkPost")
let format = "(network == %@) AND (networkPostID == %@)"
fetchRequest.predicate = NSPredicate(format: format,
network.rawValue,
networkPostID)
let posts = try! context.fetch(fetchRequest)
return !posts.isEmpty
}
我的try!
或savePosts
的{{1}}呼叫中的一个很可能是导致isPostSaved
的罪魁祸首?
如果是的话,如何缩小范围?有什么方法可以使用回溯的前三个EXC_BREAKPOINT
调用来弄清楚吗?
如果没有libswiftCore.dylib
调用,那么还有什么可能导致try!
错误?
请注意,我正在仅内存模式下使用CoreData API,因此我希望我的EXEC_BREAKPOINT
和try! context.save()
调用能够执行,除非有严重错误或编程错误。这就是为什么我使用try! context.fetch(fetchRequest)
而不是try!
的原因。