1

I have an iPad that reads data from an SQL database. The following code works fine and retrieves 2 fields from each record and reads them into an NSArray.

I now need to read 5 of the fields and I can't help but think that there is a better way of doing it rather than running 5 separate queries through php (the getinfo.php file with the choice parameter set to pick the different fields).

Any pointers to a better method for doing this?

NSString *strURLClass = [NSString stringWithFormat:@"%@%@", @"http://wwwaddress/getinfo.php?choice=1&schoolname=",obsSchoolName];
NSArray *observationsArrayClass = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLClass]];
observationListFromSQL = [[NSMutableArray alloc]init];
NSEnumerator *enumForObsClass = [observationsArrayClass objectEnumerator];

NSString *strURLDate = [NSString stringWithFormat:@"%@%@", @"http://wwwaddress/getinfo.php?choice=5&schoolname=",obsSchoolName];
NSArray *observationsArrayDate = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLDate]];
observationListFromSQL = [[NSMutableArray alloc]init];
NSEnumerator *enumForObsDate = [observationsArrayDate objectEnumerator];

id className, dateOfObs;

while (className = [enumForObsClass nextObject])
{
    dateOfObs = [enumForObsDate nextObject];
    [observationListFromSQL addObject:[NSDictionary dictionaryWithObjectsAndKeys:className, @"obsClass", dateOfObs, @"obsDate",nil]];
}

1 Answer 1

1

Yes, you can do that with less code by "folding" the statements into a loop, and using a mutable dictionary:

// Add other items that you wish to retrieve to the two arrays below:
NSArray *keys = @[@"obsClass", @"obsDate"]; // Key in the dictionary
NSArray *choices = @[@1, @5];               // Choice in the URL string
NSMutableArray *res = [NSMutableArray array];
NSMutableArray *observationListFromSQL = [NSMutableArray array];
for (int i = 0 ; i != keys.count ; i++) {
    NSNumber *choice = choices[i];
    NSString *strURLClass = [NSString stringWithFormat:@"http://wwwaddress/getinfo.php?choice=%@&schoolname=%@", choice, obsSchoolName];
    NSArray *observationsArray = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLClass]];
    NSEnumerator *objEnum = [observationsArrayClass objectEnumerator];
    NSString *key = keys[i];
    NSMutableDictionary *dict;
    if (res.count < i) {
        dict = res[i];
    } else {
        dict = [NSMutableDictionary dictionary];
        [res addObject:dict];
    }
    id item;
    while (item = [objEnum nextObject]) {
        [res setObject:item forKey:key];
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. That is neat and means less code, but it still runs the php query 5 times if I want 5 fields. Can the data not be passed back from a single call of the php file?
@RichardGriffiths That depends on the service that supplies the data: if it provides you a way to supply several choices in a single call, and defines a way for you to decode the data coming back, then the answer is "yes". Also, if there is a way to not provide any choices, get back all fields, and then choose the five fields, that you want, you might save on round-trips at the expense of receiving more data (usually a very beneficial tradeoff). Otherwise, you would need to perform multiple calls.
Brillant. Really helpful thanks. Trying your code and I get the error on dict = res[i]; and res[i] = dict; "Expected method to read array element not found on object of type 'NSMutableDictionary *". Also should the line NSEnumerator *objEnum = [observationsArrayClass objectEnumerator]; be just 'observationsArray'?
@RichardGriffiths That was supposed to be an array - I fixed that, and also put addObject in the else branch.

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.