问题描述
NSMutableuRLRequest *request = [[NSMutableuRLRequest alloc] init];
Nsstring* serverURL=getServerUrl();
NSData *postData = [NSJSONSerialization dataWithJSONObject:myPostDatanSArray options:0 error:nil];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString: serverURL]];
[request addValue:@"application/json" forHTTPHeaderField: @"Content-Type"];
[request setHTTPBody:postData];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
堆栈跟踪:
0 libsystem_trace.dylib 0x00007fff7684d8af _os_trace_read_file_at + 417
1 libsystem_trace.dylib 0x00007fff7684d944 _os_trace_read_plist_at + 46
2 libsystem_trace.dylib 0x00007fff7684929b _os_log_preferences_load + 148
3 libsystem_trace.dylib 0x00007fff7684a00a _os_log_preferences_refresh + 110
4 libsystem_trace.dylib 0x00007fff76849bb6 os_log_create + 999
5 libsqlite3.dylib 0x00007fff75e0a834 sqlite3_initialize + 1364
6 libsqlite3.dylib 0x00007fff75e08c02 openDatabase + 114
7 com.apple.CFNetwork 0x00007fff4d5d6fdb -[NSURLCacheDBReader _openDBReadConnections] + 140
8 com.apple.CFNetwork 0x00007fff4d5d6f2b -[NSURLCacheDBReader openAndPrepareReadCacheDB] + 22
9 com.apple.CFNetwork 0x00007fff4d4babd2 __CFURLCache::createNSURLStorageClient(__CFString const*,long,std::__1::shared_ptr<__CFURLCache>) + 166
10 com.apple.CFNetwork 0x00007fff4d5bb052 __CFURLCacheCreateInternal(__CFAllocator const*,__CFString const*,bool,bool) + 633
11 com.apple.CFNetwork 0x00007fff4d4ba480 -[NSURLCache initWithMemoryCapacity:diskCapacity:diskPath:] + 168
12 com.apple.CFNetwork 0x00007fff4d4ba27b +[NSURLCache sharedURLCache] + 96
13 com.apple.CFNetwork 0x00007fff4d4c7fa8 -[__NSURLSessionLocal _createXURLCache0] + 40
14 com.apple.CFNetwork 0x00007fff4d4c7f34 -[__NSURLSessionLocal _createXURLCache] + 71
15 com.apple.CFNetwork 0x00007fff4d4df282 URLConnectionClient::URLConnectionClient(ClassicURLConnection*,NSURLSessionTask const*,dispatch_queue_s*) + 150
16 com.apple.CFNetwork 0x00007fff4d4df18b URLConnectionClient_Classic::URLConnectionClient_Classic(ClassicURLConnection*,CFURLConnectionClient_V1 const*,dispatch_queue_s*) + 21
17 com.apple.CFNetwork 0x00007fff4d4df02f ClassicURLConnection::initialize(CFURLConnectionClient_V1*,NSURLSessionTask const*) + 145
18 com.apple.CFNetwork 0x00007fff4d4ddecc ClassicURLConnection::initialize(_CFURLRequest const*,_CFURLRequest*,CFURLConnectionClient_V1*,__CFDictionary const*) + 216
19 com.apple.CFNetwork 0x00007fff4d4ddc64 CFURLConnectionCreateWithProperties + 297
20 com.apple.CFNetwork 0x00007fff4d5121cc CFURLConnectionCreate + 52
21 com.apple.CFNetwork 0x00007fff4d5ce4c9 Syncclient::Syncclient(__CFAllocator const*,_CFURLRequest const*,dispatch_queue_s*,void (__CFData const*,_CFURLResponse*,__CFError*) block_pointer) + 213
22 com.apple.CFNetwork 0x00007fff4d5ce1d4 CFURLConnectionSendSynchronousRequest + 326
23 com.apple.CFNetwork 0x00007fff4d5e58a4 +[NSURLConnection sendSynchronousRequest:returningResponse:error:] + 117
我还可以在崩溃日志中看到以下信息:
应用特定信息:
LIBTRACE 客户端的错误:不要关闭随机文件
我是目标 c 的新手。提前感谢您抽出宝贵时间。
解决方法
看起来 NSURL 共享缓存使用 SQLite 作为其磁盘存储。我不知道。有趣。
我猜这个调用不会发生在主线程上。如果这是真的,那么这个回溯会给我带来许多危险信号,因为默认情况下 SQLite 不是线程安全的,而且我已经看到了令人着迷的数据库损坏和其他错误,但人们认为它是。
在生成此代码之前,我会尝试从您的主线程显式调用 [NSURLCache sharedURLCache]
,然后查看问题是否消失。如果没有,您的磁盘上可能有损坏的缓存数据库。