views:

35

answers:

0

Hi All,

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