0

I have a NSMutableArray in an object. In an object-method, I do something like this:

/* ... */
[[LRResty client] get:connectURL withBlock:^(LRRestyResponse *r) {
    SBJsonParser *jsonParser = [SBJsonParser new];
    NSDictionary *jsonResponse = [jsonParser objectWithString:[r asString]];
    NSDictionary *permittedBases= [jsonResponse objectForKey:@"permittedBases"];
    Database *database = [[Database alloc] init];

    for (id key in permittedBases) {
        /* ... */
        [workingDatabases addObject:database];
    }
}];

return workingDatabases;    

At the return line, there are no objects in my array (anymore). I am aware of the fact, that the 'database'-objects are going out of scope. But I am saving them in the array.

Am I overseeing something?

If it is of any help, here is the header file:

@class Database;

@interface CommunicationHelper : NSObject {
    NSMutableArray *workingDatabases;
}


// The function where the problem appears:
- (NSMutableArray *)getDatabasesForWebsite:(Website *)websiteIn; 

@property(nonatomic,copy) NSMutableArray *workingDatabases;


@end

3 Answers 3

1

just allocate your workingDatabases (Mutable array) somewhere before using that array.

Once you allocate it,It will work fine.

Sign up to request clarification or add additional context in comments.

2 Comments

That's not the problem. I allocated it in the init method.
@MarcBackes Actually this was the same problem faced by me when i was working around mutable array. i also allocated the mutable array in my viewDidLoad function but i was still getting the out of scope thing. So what i did i allocated the array in that same function and release it when its job gets over. just try to alloc your mutable array in the same function where you are using it or rather that allocating it in "init" load it in viewDidLoad..just give it a try.:)
1

I assume it's because [LRResty client] get: is asynchronous. The block is called when the connection is finished, i.e. after the call to return.

//Called first
[[LRResty client] get:connectURL

//Called second
return workingDatabases;

//Called later when the connection is finished
SBJsonParser *jsonParser = [SBJsonParser new];
    NSDictionary *jsonResponse = [jsonParser objectWithString:[r asString]];
    NSDictionary *permittedBases= [jsonResponse objectForKey:@"permittedBases"];
    Database *database = [[Database alloc] init];

    for (id key in permittedBases) {
        /* ... */
        [workingDatabases addObject:database];
    }

Edit

Ajeet has a valid point too, ensure your array is initialized.

Comments

1

I used the LRResty framework for accessing a RESTful webservice. It was an odd thing anyways, so I switched to a way more rich-featured framework, called "ASIHTTP". I would recommend that to anyone who wants to use RESTful services (and more) on iOS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.