简体   繁体   English

iPhone应用程序照片从应用程序线程上传到服务器

[英]iphone app photo upload to server from app threads

I have an app that needs to upload a least 5 photos to a server using API call available with the server. 我有一个应用程序需要使用服务器可用的API调用将至少5张照片上传到服务器。 For that I am planning to use threads which will take care of photo upload and the main process can go on with the navigation of views etc. What I cant decide is whether it is OK to spawn five separate threads in iphone or use a single thread that will do the upload. 为此,我计划使用线程来处理照片上传,并且主要过程可以继续进行视图导航等。我无法决定的是可以在iPhone中生成五个单独的线程还是可以使用单个线程将会进行上传。 In the later cases obviously it will become quite slow. 在后面的情况下,显然它将变得非常缓慢。 Basically an HTTP POST request will be made to the server with the NSMutableURLRequest object using NSCOnnection. 基本上,将使用NSCOnnection和NSMutableURLRequest对象向服务器发出HTTP POST请求。

More threads mean more complexity and sync issues, but I can try to write code as neat as possible if it means better performance than a single thread which is simple but is a real stopper if performance is considered. 更多的线程意味着更多的复杂性和同步问题,但是如果它意味着比单个线程更好的性能(如果考虑到性能,这是一个简单的障碍),那么我可以尝试编写尽可能整洁的代码。 Anybody with any experience in this kinda app. 任何有这种经验的人。 ?? ??

I would suggest using one extra thread and queuing the uploads, one after the other. 我建议使用一个额外的线程并逐个排队上载。 You'll end up clogging the network interfaces if you try 5 uploads concurrently. 如果同时尝试5次上传,最终将阻塞网络接口。 Remember that the iPhone will often be on a 3G or even EDGE connection, not always WiFi, so photo uploads can be really slow, and even slower if there are 5 at once. 请记住,iPhone通常会通过3G甚至是EDGE连接,而不是始终通过WiFi进行连接,因此照片上传的速度可能会非常慢,甚至一次只能上传5张照片。

You could probably benefit from using NSOperation and NSOperationQueue to nicely handle the ugly threading for you. 您可能会受益于使用NSOperation和NSOperationQueue为您很好地处理丑陋的线程。 Wrap up an upload process into an NSOperation, and then queue 5 of them for each image. 将上载过程包装到NSOperation中,然后将每个图像中的5个排队。 Should work quite nicely. 应该工作得很好。

Jasarien thanks for the bit on NSOperation and NSOperationQueue. Jasarien感谢您对NSOperation和NSOperationQueue的关注。 It did simplyify in great measure. 确实在很大程度上证明了这一点。 But right now I hve run into a major issue. 但是现在我遇到了一个重大问题。 I spawn an extra thread from the main thread. 我从主线程中产生了一个额外的线程。 This thread queues up each of the picture upload operation. 该线程将每个图片上传操作排队。 So this thread queues up 5 picture opload operations in a queue. 因此,该线程将5个图片opload操作排入队列。 This is absolutely fine when working with Mac PC. 使用Mac PC时,这绝对好。 Now when I push the app to the device, only one pic upload is successful and the rest fails. 现在,当我将应用程序推送到设备上时,只有一个图片上传成功,其余的失败。 Most of the cases of failure are due to server time out error. 大多数失败情况是由于服务器超时错误引起的。 So basically I am wondering, whether NSOperationQueue ensure only one operation at a time or not ? 所以基本上我想知道,NSOperationQueue是否一次只能确保一个操作? I mean if the first pic upload is in progress and say the next operation is already added in the queue. 我的意思是,如果第一个图片上传正在进行中,并且说下一个操作已经添加到队列中。 Would it create an extra thread for the second operation too while the first one is running ? 在第一个操作运行时,它还会为第二个操作创建额外的线程吗? I think as the name suggests, it must wait in the queue until the previous one is done. 我认为,顾名思义,它必须在队列中等待,直到上一个完成为止。 Not sure how to go abt doing it. 不知道如何去做。 I am uploading pic which are taken with iphone camera. 我正在上传用iPhone相机拍摄的照片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM