【发布时间】:2019-04-10 21:48:10
【问题描述】:
应用在 Apple 团队审核时崩溃,但在开发时无法重现崩溃。
我尝试在所有可能的模拟器中运行它,还尝试了 Amazon AWS Device Farm 和 2 种不同的真实设备(iPad 和 iPhone)。
DateFormater 似乎无法解析日期并返回 nil,在此之前我们已经解析了一些其他字段,但没有日期。
我们认为格式应该是指定的(我们可以在我们的设备上解析它)并且我们使用与苹果审核团队相同的帐户。
我们还尝试将日历切换为非公历,这也没有使应用程序崩溃。
应用在self.modified = ... 行崩溃
来自 Event.swift ...
import SwiftyJSON
...
required init(json: JSON) {
self.id = json["tm_event_id"].int!;
self.name = json["name"].string!;
self.eventStatus = EventStatus.init(rawValue: json["event_status"].string!)!;
self.modified = AppDelegate.formaterDatetime.date(from: json["modified"].string!)!;
self.dateFrom = AppDelegate.formaterDate.date(from: json["date_from"].string!)!;
...
}
来自 AppDelegate.swift ...
static var formaterDatetime:DateFormatter {
let f = DateFormatter();
f.dateFormat = "yyyy-MM-dd HH:mm:ss";
f.timeZone = dataTimeZone
f.locale = Calendar.current.locale;
return f;
}
后端响应(基于我们的服务器日志)
[
{
"tm_event_id": 1082,
"company_id": 16,
"organization_id": 58,
"org_user_id": 126,
"org_location_id": 93,
"name": "My Test",
"date_from": "2019-03-25",
"date_to": "2019-03-30",
"week_ending": "2019-03-31",
"placement_count": 6,
"notes": "",
"event_status": "private",
"created": "2019-03-04 13:25:25",
"modified": "2019-03-04 13:25:25",
"subrequirements": [ ... ],
"timesheets": [ ... ]
}
崩溃日志
Crashed: com.apple.root.user-initiated-qos
0 YouRecruit Work Tracker 0x1028b9470 specialized Event.init(json:) (Event.swift:224)
1 YouRecruit Work Tracker 0x10286424c specialized static StorableItem.initStatic(json:) (Event.swift)
2 YouRecruit Work Tracker 0x102844b20 closure #3 in _TempManAPI.().init() (<compiler-generated>)
3 YouRecruit Work Tracker 0x102844c4c specialized thunk for @escaping @callee_guaranteed (@guaranteed Entity<JSON>) -> (@owned [Event]?, @error @owned Error) (<compiler-generated>)
4 Siesta 0x102e22a0c (Missing)
5 Siesta 0x102e2250c (Missing)
6 Siesta 0x102df4700 (Missing)
7 Siesta 0x102df9c70 (Missing)
8 Siesta 0x102df96fc (Missing)
9 Siesta 0x102df163c (Missing)
10 Siesta 0x102de9fbc (Missing)
11 libdispatch.dylib 0x222a44a38 _dispatch_call_block_and_release + 24
12 libdispatch.dylib 0x222a457d4 _dispatch_client_callout + 16
13 libdispatch.dylib 0x2229f6160 _dispatch_root_queue_drain + 680
14 libdispatch.dylib 0x2229f68d0 _dispatch_worker_thread2 + 128
15 libsystem_pthread.dylib 0x222c251b4 _pthread_wqthread + 464
16 libsystem_pthread.dylib 0x222c27cd4 start_wqthread + 4
知道如何在本地开发人员上重现崩溃或什么会导致崩溃?
【问题讨论】:
-
与您的问题无关,但不要在 Swift 的行尾使用
;,这不是 Objective-C。此外,您应该使用Codable来解码 JSON,并且从 JSON 中强制展开值是一个非常糟糕的主意。如果这是完整的堆栈跟踪和完整的init(json:),那么崩溃很可能来自强制展开。 -
您尝试过 TestFlight 构建吗?
-
不要使用强制解包。这不是管理代码的专业方式。如果您将构建提交到应用商店,请确保代码中的强制解包为 0。
-
为什么它只会在使用 Apple(至少两次)并且正确读取之前的值时才会崩溃?
-
可能是应用程序崩溃了,因为他们在各种网络条件下对其进行了测试,并且服务器返回了与预期不同的 json 格式(或空数据)。或者也许一些整数被编码为字符串正如其他人所建议的那样,不要使用强制解包,在某些情况下使用可选的 init() 是很好的,这样你就可以处理错误的 json 数据。当我看到日期时,我建议您使用带有时区的日期时间,因为您的用户可能在世界各地,而他们的 4 月 5 日可能是其他 4 月 4 日。