iPhone 在新线程中上传
我一直在尝试在我的应用程序中实现一个后台线程来完成长途、硬移植上传的工作。
我正在使用 FBConnect SDK,它一直运行良好,但我希望 GUI 端保持平稳运行,目前应用程序在开始上传之前有明显的暂停。
目前发生的情况是我将上传作业添加到队列中,然后运行“startQueue”。
这将从队列中取出第一个作业,调整图像大小并开始上传到 Facebook。
只有在发出 URL 请求时,它才开始在自己的线程中运行,然后我才能重新获得 GUI 的控制权。
我想要的是能够将作业添加到队列中,然后在单独的线程中运行“startQueue”。这将立即让我重新获得 GUI 的控制权。
发生的情况是我立即返回 GUI,并且图像大小被调整(在新线程中)并且 Facebook 上传开始(也在新线程中),但随后什么也没有。
对于这个子线程中可以做什么和不能做什么是否有某种限制?是否不允许 URL 连接?手机上似乎没有任何错误,我可以毫无问题地继续将作业添加到队列中,但初始上传未完成。
init 方法创建线程...
bgThread = [[NSThread alloc] initWithTarget:self selector:@selector(startQueue) object:nil];
然后 add job 方法启动它...
[bgThread start];
然后按如下方式运行...
-(void)startQueue
{
running = TRUE;
bgPool = [[NSAutoreleasePool alloc] init];
[self startJob];
}
-(void)startJob
{
NSDictionary *currentJob = [NSDictionary dictionaryWithDictionary:[jobQueue objectAtIndex:0]];
NSArray *networks = [currentJob objectForKey:@"networks"];
NSLog(@"Resizing image");
NSLog(@"Starting Uploads");
for (int i = 0; i < [networks count]; i++)
{
NSString *networkName = [networks objectAtIndex:i];
if ([networkName isEqualToString:@"Facebook"])
{
NSLog(@"Facebook");
currentUploads += 1;
[self.fbDelegate uploadPhoto:uploadImage withCaption:[currentJob objectForKey:@"caption"]];
}
}
}
不使用线程(即仅运行 startJob),一切都可以完美运行。当我调用 bgThread start 时,它运行完美,我得到了 Facebook NSLog,但工作永远不会完成。它甚至没有以错误结束。
感谢您提供的任何指示或提示!
奥利弗
I've been trying to implement a background thread in my app to do the long haul, hard graft uploading stuff.
I'm using the FBConnect SDK and it has been working perfectly but I would like to keep the GUI side working smoothly and at the moment there is a noticeable pause in the app before it starts the upload.
What happens at the moment is I add an upload job to the queue and then run "startQueue".
This take the first job from the queue, resizes the image and starts the upload up to Facebook.
It is only at the point at which the URL request is made that it starts running in its own thread and I get back control of the GUI.
What I'd like is to be able to add a job to the queue and then run "startQueue" in a separate thread. This would instantly give me back control of the GUI.
What is happening is I get back the GUI straight away and the image is resized )in the new thread) and the Facebook upload starts (also in the new thread) but then nothing.
Is there some sort of restriction as to what can and can't be done in this child thread? Are URL connections not allowed? There don't appear to be any errors on the phone and I can carry on adding jobs to the queue with no problems but the initial upload just doesn't complete.
init method creates the thread...
bgThread = [[NSThread alloc] initWithTarget:self selector:@selector(startQueue) object:nil];
Then the add job method starts it...
[bgThread start];
then this runs as so...
-(void)startQueue
{
running = TRUE;
bgPool = [[NSAutoreleasePool alloc] init];
[self startJob];
}
-(void)startJob
{
NSDictionary *currentJob = [NSDictionary dictionaryWithDictionary:[jobQueue objectAtIndex:0]];
NSArray *networks = [currentJob objectForKey:@"networks"];
NSLog(@"Resizing image");
NSLog(@"Starting Uploads");
for (int i = 0; i < [networks count]; i++)
{
NSString *networkName = [networks objectAtIndex:i];
if ([networkName isEqualToString:@"Facebook"])
{
NSLog(@"Facebook");
currentUploads += 1;
[self.fbDelegate uploadPhoto:uploadImage withCaption:[currentJob objectForKey:@"caption"]];
}
}
}
Without using the thread (i.e. just running startJob) it all works perfectly. When I call bgThread start it runs perfectly and I get the Facebook NSLog but the job never finishes. It doesn't even finish with an error.
Thanks for any pointers or tips that you may be able to provide!
Oliver
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论