1
- (void)request:(FBRequest *)request didLoad:(id)result {


        // NSArray *resultData = [result objectForKey:@"data"];
        NSMutableArray *photos = [[NSMutableArray alloc] initWithCapacity:1];
        //  imageArray = [[NSMutableArray alloc] initWithCapacity:1];
        NSArray *resultData = [result objectForKey:@"data"];
        if ([resultData count] > 0) 
        {
            noOfImages = 0;

            for (NSUInteger i=0; i<[resultData count] ; i++) 
            {
                [photos addObject:[resultData objectAtIndex:i]];
                NSDictionary *albumPhotos = [photos objectAtIndex:i];
                NSString *imageUrl = [albumPhotos objectForKey:@"picture"];
                NSLog(@"ImageURL:%@",imageUrl);
                NSURL *url = [NSURL URLWithString:imageUrl];
               //  [slideImageArray addObject:url];
                NSData *data = [NSData dataWithContentsOfURL:url];
                UIImage *images = [UIImage imageWithData:data];
               // imageView.image = images;
                [slideImageArray addObject:images];

                [NSThread detachNewThreadSelector:@selector(startTheBackgroundJob:) toTarget:self withObject:images];





            }

            NSLog(@"ImageCount:%d",[slideImageArray count]);  

            if([slideImageArray count] == 1)
            {
                imageView.image = [slideImageArray objectAtIndex:0];
            }

            if([slideImageArray count]>1)
            {
                numTimer =0;     
                myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerRunning) userInfo:nil repeats:YES];
            }



        }






}



- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
    NSLog(@"Err message: %@", [[error userInfo] objectForKey:@"error_msg"]);
    NSLog(@"Err code: %d", [error code]);
  }

- (void)startTheBackgroundJob:(UIImage *)img 
{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    // wait for 3 seconds before starting the thread, you don't have to do that. This is just an example how to stop the NSThread for some time
    //imageView.image = img;
    noOfImages++;
    // [NSThread sleepForTimeInterval:3];
   // [slideImageArray addObject:img];

    //[NSThread sleepForTimeInterval:1];
     imageView.image = img;
    if(noOfImages == 1)
    {
        NSLog(@"noValueif:%d",noOfImages);
       // imageView.image = [slideImageArray objectAtIndex:0];
        [NSThread sleepForTimeInterval:1];
       // imageView.image = img;
    }

    else if(noOfImages >1)
    {
        NSLog(@"noValue:%d",noOfImages);
//        numTimer =0;     
//        myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerRunning) userInfo:nil repeats:YES];
        // imageView.image = img;
        [NSThread sleepForTimeInterval:3];

    }



  //  [self performSelectorOnMainThread:@selector(changeImageViewImage) withObject:nil waitUntilDone:NO];
    [pool release];

}

I have to run a background thread for getting image from Facebook album .i have to start a slide show with the first image collected.and i given a sleep for 3 second but it is not working.Also is there any other method for getting image from url other than the NSData *data = [NSData dataWithContentsOfURL:url]; method

Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
Meera
  • 77
  • 6

1 Answers1

0

Did you ever find the answer for this question?? or were u able to solve it?

And for this facebook api, are you using a personalized subclass of NSThread or using Dispatch queues?

And to answer your question if you want to fetch data in the background use performSelectorONMainThread... some facebook SDK methods require to be runned on the main thread for them to work, in my personal experience i found out that most of the data requesting is necessary to be on the mainthread using this method.

It worked for me, hopefully it works for you :).

S.H.
  • 2,833
  • 3
  • 26
  • 28