我的应用程序显示 OpenStreetMap 切片(256*256 图像),并在用户浏览 map 时将它们缓存在磁盘上。
在 iOS 10 上,一切都很好,但在 iOS 8 上,如果我不以原子方式编写 NSData,应用程序会崩溃 (EXC_BAD_ACCESS)。以原子方式执行它没有问题,但我想了解发生了什么。
这是我正在使用的相关代码:
private func putInCache(key:NSString, data:NSData) {
// Get the path:
let path:String = "\(self.imagesFolderPath)/\(key)";
var success:Bool = false;
// Save the image, if it does not exists:
if(!FileManager.default.fileExists(atPath:path)) {
// Run in background:
DispatchQueue.global().async {
// Put the image in the memory cache:
self.memoryCache.setObject(data, forKey:key);
// Insert the row in the database:
let success = insertDataInDb(...);
// Then save the file (if the DB insertion succeeded):
if(success) {
data.write(toFile:path, atomically:false); // If I set atomically to true, it works.
}
}
}
}
有人知道为什么以原子方式写入数据之间存在差异吗?
Best Answer-推荐答案 strong>
来自 Apple 文档 (https://developer.apple.com/reference/foundation/nsdata):
Atomic writes guarantee that the data is either saved in its entirety,
or it fails completely. The atomic write begins by writing the data to
a temporary file. If this write succeeds, then the method moves the
temporary file to its final location.
您可能会遇到崩溃,因为不同队列同时尝试写入同一路径。
关于ios - NSData.write 在 iOS 8 上触发 ECX_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/40090845/
|