这个Swift iPad崩溃日志是什么意思? [英] What does this Swift iPad crash log mean?

查看:230
本文介绍了这个Swift iPad崩溃日志是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

App在每台符合9.3+ 部署目标的设备上运行良好,但 iPad2除外。网址很好。适用于所有iPhone和其他所有iPad。崩溃发生在物理iPad2和模拟器iPad2 iOS 9.3上。

App runs fine on every device that qualifies for deployment target of 9.3+ except iPad2. The url is good. Works on every iPhone and every other iPad. The crash is on a physical iPad2 and Simulator iPad2 iOS 9.3.

    doc.save(to: target, for: .forCreating, completionHandler: {(success) in
        if (success) {
            print("Save succeeded")
            }
        } else {
            print("Save failed")
        }
    })

这是崩溃的地方。到达此行的断点,并且不要在完成处理程序或打印中找到断点。再一次,只是iPad的一个型号。

This is where it crashes. Get to a breakpoint at this line, and do not get to breakpoints in completion handler or either print. Again, just the one model of just iPad.

崩溃日志已经超出我的想象。对你们任何人都有意义吗?谢谢。

The crash log is over my head. Does it make sense to any of you? Thank you.


编辑:扩展崩溃日志


推荐答案

tl; dr:这是一个错误Swift数据类型。解决方案:在您的UIDocument 内容(forType :))中,更改此项:

tl;dr: It's a bug in the Swift Data type. Solution: In your UIDocument contents(forType:) implementation, change this:

return data

到此:

return NSData(data:data)






更多信息:请注意,仅向NSData投放数据无济于事:


More info: Note that merely casting data to NSData won't help:

return data as NSData

那不会让我们更进一步,因为这是问题的桥梁。我们已经桥接到NSData并且没有帮助。你必须创建一个不是Swift Data对象的全新对象。

That doesn't get us any further because it's the bridging that's the problem. We're already bridging to NSData and it isn't helping. You have to create a completely new object that is not a Swift Data object.

更多信息:对于后来出现并希望对其进行测试的后代,可以如下可靠地再现崩溃。像往常一样创建一个新的Single View项目,包含一个app delegate和一个ViewController。在 ViewController.swift 文件中,输入:

Even more info: For future generations who come along and want to test this, the crash can be reliably reproduced as follows. Make a new Single View project, with an app delegate and a ViewController as usual. In the ViewController.swift file, put this:

import UIKit

class WhatsUpDoc: UIDocument {
    var array = [String]()
    override func load(fromContents contents: Any, ofType typeName: String?) throws {}
    override func contents(forType typeName: String) throws -> Any {
        let data = NSKeyedArchiver.archivedData(withRootObject: array)
        return data // comment out this line to avoid the crash
        return NSData(data:data)
    }
}

class ViewController: UIViewController {
    var doc : WhatsUpDoc?
    override func viewDidLoad() {
        super.viewDidLoad()
        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let baseURL = documentsDirectory.appendingPathComponent("Untitled")
        let doc = WhatsUpDoc(fileURL: baseURL)
        self.doc = doc
        self.doc!.save(to:self.doc!.fileURL, for: .forCreating)
    }
}

将项目配置为部署目标为9.0,并确保您手头有9.0模拟器SDK。在Window> Devices中,给自己一个iPad 2模拟器。将该模拟器设置为项目的目标。跑,崩溃。评论说要评论它的线,并且不要崩溃。

Configure your project to have a deployment target of 9.0, and make sure you've got a 9.0 simulator SDK on hand. In Window > Devices, give yourself an iPad 2 simulator. Set that simulator as the project's destination. Run, and crash. Comment out the line that says to comment it out, and don't crash.

分析后的问答;答:

等等,那究竟是什么_的错误?你不是说你不能把Swift Data对象写到磁盘上,是吗?

不,这与特殊的线程有关在UIDocument文件保存期间写入的本质。请注意,我们在后台线程(OP的屏幕截图中的线程10或11)中崩溃。我们在中崩溃的方法是 SwiftNSData 的方法,由Swift Data包装的NSData。我们试图在这个后台线程上枚举数据的字节,但我们不能;据推测,这在某些设备类型(即32位设备)上不是线程安全的。我的解决方案是完全从图片中获取Swift数据。

No, it's got something to do with the peculiar threaded nature of writing during a UIDocument file save. Note that we are crashing in a background thread (thread 10 or 11 in the OP's screen shots). And what we are crashing in is a method of SwiftNSData, the NSData wrapped by a Swift Data. We're trying to enumerate the data's bytes on this background thread, and we can't; presumably this isn't thread-safe on certain device types, i.e. 32-bit devices. My solution is to get Swift Data out of the picture completely.

好的,所以事实证明这个bug是已知的;答案在这里给出: https://stackoverflow.com/a/41163395/341994 那怎么样?

Okay, so it turns out this bug is known; the answer is given here: https://stackoverflow.com/a/41163395/341994 How about that?

好的,但我在回答当前问题时独立地想出来了。直到后来我才发现搜索时,我才发现其他问题和答案。所以我把当前的问题标记为重复,但是我的答案是出于历史目的,特别是。因为它提供了一个明确的测试用例(另一个问题没有)。

Okay, but I figured it out independently in response to the current question. I didn't find out about the other question and answer until later, when it occurred to me to do a search. So I've marked the current question as a duplicate, but I'm leaving my answer for historical purposes, esp. as it gives a clear test case (which the other question does not).

顺便说一句,我不知道为什么要使用 NSMutableData(数据) :)会比我使用 NSData(data :) 的解决方案更好,但这就是Apple所说的,所以让我们把它当成福音。

By the way, I do not know why using NSMutableData(data:) would be better than my solution of using NSData(data:), but that's what Apple says to do, so let's just take it as gospel.

这篇关于这个Swift iPad崩溃日志是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆