RK Ekalaya
RK Ekalaya

Reputation: 31

About asynchronous connection in AFNetworking : how to add the response data to NSMutable Array

im a noob and this is my first post here. I tried to load images from URL and assign it to some NSMutableArray or UIImage variable, but it failed. I knew that asynchronous things in AFNetworking has been discussed a lot, but still im missing something. here is my sample code

-(void)sampleCode{
NSURL *theUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost/images/054.jpg"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:theUrl];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request
  imageProcessingBlock:nil
   success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
       //this code success
       NSLog([NSString stringWithFormat:@"--%@--", image]);
       UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)];
       imgV.image=image;
       [self.view addSubview:imgV];
       //End code success. Nslog and image appear

       //this code fail
       [self.someArray addObject:image];
       [self.someArray addObject:@"xx"];
       self.someImage = image;
       NSLog([NSString stringWithFormat:@"%@ --",self.someArray]);
       //end code fail. self.someArray and self.someImage null.
   }
   failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
   }];
[operation start];

the code can use the image in success block. but i still have to process the image, so i need to assign it into global variable (in case NSMutableArray *someArray or UIImage *someImage).

my question is : if i can use it in success block, it means that the image is arrived, but why it cant assign to global variable?

when i use UITableViewControler, i can use self.tableView reloadData. when i dont use tableView, how to "reloadData"?

-- Edit --

- (void)viewDidLoad
{
[super viewDidLoad];
self.someArray = [[NSMutableArray alloc]init];

[self sampleCode];
NSLog([NSString stringWithFormat:@"Array - %@",self.someArray]);
// its still empty
}

Can I use it that way? it still empty

thanks

Upvotes: 2

Views: 537

Answers (4)

Divyam shukla
Divyam shukla

Reputation: 2048

If you will not use the table view then you have to create your own method to set the data after getting the data from the url.

Upvotes: 0

iSpark
iSpark

Reputation: 952

you are doing fine,but i think as prince said you forgotten to alloc your array self.someArray

self.someArray = [[NSMutableArray alloc]init];//in ViewDidLoadMethod


NSURL *theUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://images.mathrubhumi.com/english_images/2012/Dec/06/03082_185786.jpg"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:theUrl];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil
                                      success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                                      UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)];
                                      imgV.image = image;
                                      [self.view addSubview:imgV];

                                      [self.someArray addObject:image];
                                      [self.someArray addObject:@"xx"];
                                      NSLog(@"arr--%@",self.someArray);
                                      imageView.image = image;// here imageView is globalVariable

                                      }
                                     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                     }];
[operation start];

This is giving self.someArray with values and imageView is showing its image.

Upvotes: 0

Paresh Navadiya
Paresh Navadiya

Reputation: 38249

Reason is array not allocated.

Use these line in viewDidLoad: method for allocating and ready to be used anywhere in class

self.someArray = [[NSMutableArray alloc]init];

EDIT : Reloading of data depends on your requirement. For example say u have UIImageView *imgView;

imgView.image = [UIImage imageNamed:@"someImage"];

Now after successfully getting upadated will be:

imgView.image = someImageDownloaded using AFImageRequestOperation

Upvotes: 1

danypata
danypata

Reputation: 10175

First of all, is your self.someArray initialized ?

For reloadData without a UITableView, you can call methods that will load your data in the success block and methods that are handling the errors (for example show a message to the user or change the data processing according to the given error) in the failure block.

In general if the success block is called that means that the operations was successful and in your case means that the request for the image was successful but that doesn't always mean that the image was received, it is possible that the server returns a success message but without an image and in this case the image will be nil but the success block will be called.

Upvotes: 0

Related Questions