gpt4 book ai didi

iOS NSURLSessionUploadTask 响应数据

转载 作者:行者123 更新时间:2023-12-01 18:58:28 28 4
gpt4 key购买 nike

我已经成功实现了 NSURLSessionUploadTask 并在后台和前台工作。但是读取响应数据时出现问题。

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{

NSLog(@"1 DATA:\n%@\nEND DATA\n", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);

[self.responseData appendData:data];
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
if (!error) {
NSLog(@"AT THE END DATA:\n%@\nEND DATA\n", [[NSString alloc] initWithData: self.responseData encoding: NSUTF8StringEncoding]);

[self parsingJSONResponse:self.responseData];

} else {
NSLog(@"HTTP uploading error : %@", error);
}
}

这些是上述两个 NSLog 的输出

1 DATA: {"success":true,"data":[{"uuid":"8BE7DF37-9DA1-44D2-B48C-D012F699A9B1","id":266626},{"uuid":"3406D865-1A41-4FC6-BA0B-0638F17757CC","id":266656}],"errors":[],"entityName":"LeadProfile"} END DATA

AT THE END DATA: {"success":true,"data":[{"uuid":"8BE7DF37-9DA1-44D2-B48C-D012F699A9B1","id":266626},{"uuid":"3406D865-1A41-4FC6-BA0B-0638F17757CC","id":266656}],"errors":[],"entityName":"LeadProfile"}{"success":true,"data":[{"uuid":"8BE7DF37-9DA1-44D2-B48C-D012F699A9B1","id":266626},{"uuid":"3406D865-1A41-4FC6-BA0B-0638F17757CC","id":266656}],"errors":[],"entityName":"LeadProfile"} END DATA



我想知道为什么这会给我一个上传任务两个不同的响应。每个位置的 self.responseData 有何不同?

有人认为这是因为苹果网站上提到的原因吗? (因为 NSData 对象通常是由多个不同的数据对象拼凑而成,所以尽可能使用 NSData 的 enumerateByteRangesUsingBlock: 方法来遍历数据而不是使用 bytes 方法(将 NSData 对象扁平化为单个内存块) developer.apple.com

最佳答案

你问:

I wonder why this is giving me two different responses for one upload task. How the self.responseData can be different in each location ?



无疑是因为 responseData没有正确或在正确的时间实例化。 (我倾向于在 didReceiveResponse 中这样做。)注意,你不是在看 responseData在您的 didReceiveData .您正在查看 data .我建议检查 responseData在附加 data 之后立即在 didReceiveData ,我相信你也会在那里看到它翻倍。问题是为什么它没有正确实例化/初始化。

Is anyone think this is because of the reason mention on Apple website?

"Because the NSData object is often pieced together from a number of different data objects, whenever possible, use NSData’s enumerateByteRangesUsingBlock: method to iterate through the data rather than using the bytes method (which flattens the NSData object into a single memory block)."



不,这是一个完全不相关的问题。我敢肯定这里的问题要平凡得多。

不幸的是,我们这里没有足够的代码来诊断确切的问题。

关于iOS NSURLSessionUploadTask 响应数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24802257/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com