0

I have the following values in my SQLite Database;

Column1------ Column2

"1" ------- "One"

"2" -------"Two"

"3" -------- "Three" 

etc..

Now i will need to read all these values and save it into a NSArray, NSMutableArray etc. But when i store it in one of the above mentioned arrays the order of data should not change. It should always remain as it was in the database.

So my question is that to what should i save it to without changing the order of the data ? Is it a NSArray or a NSMutableArray ?

The Query would be a simple select All statement;

FMResultSet *results = [db executeQuery:@"SELECT * FROM PersonTable"];


        while([results next]) 
        {

            Name *nam= [[Name alloc] init];

            nam.fname= [results stringForColumn:@"fname"];
            nam.lname= [results stringForColumn:@"lname"];

            [mutArray addObject:nam];  
        }

        [db close];

    }else {
        NSLog(@"DB Close");
    }

Finally I will be returning a MutableArray, in this case it's mutArray

3 Answers 3

1

The code you use to create your array is fine: it'll make an NSMutableArray with the values you want, in the order that they were received. NSMutableArray is an ordered collection unlike NSSet, so the ordering will be preserved unless you do something to change it.

I'm a little confused by what you mean by this:

But when i store it in one of the above mentioned arrays the order of data should not change. It should always remain as it was in the database.

Obviously, if you use an NSArray, it's immutable, so the contents and ordering of that array will never change. If you want to take the NSMutableArray and make it immutable just do NSArray *immutableCopy = [mutArray copy] and you'll be set.

Still, even if you used an NSMutableArray, it's not like the array is going to modify itself for no reason while your app is running. Unless you make explicit calls to mutate the array, with calls like addObject: removeObjectAtIndex:, and sortUsingDescriptors:, the array isn't going to change.

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

2 Comments

So you suggest that my code is fine, and i should not make any changes to it ?
Or you could use copy to make it immutable. There would be nothing wrong with doing that. I'm just pointing out that you seem excessively worried about the order of the data changing later on. That's not something that'll happen unless you explicitly do it.
1

Ordering of data does not depends on data type. If you need add/delete/replace any value from array you should use NSMutableArray, otherwise you can use NSArray.

2 Comments

How can i save values to a NSMutableArray or NSArray in the order i had inserted to the sqlite database. Can you show me an example ?
try some thing like this: SELECT * FROM PersonTable ORDER BY lname. Here after order by use the column name, by which you want to order.
1

for storing the value in the same order as it is in database you can use dictionary in below format

FMResultSet *results = [db executeQuery:@"SELECT * FROM PersonTable"];

   int i = 0;
    while([results next]) 
    {

        Name *nam= [[Name alloc] init];

        nam.fname= [results stringForColumn:@"fname"];
        nam.lname= [results stringForColumn:@"lname"];

        [myDictionary setObject:nam forKey:[NSString StringWithFormat:@"%d",i]];  
        i++;
    }

    [db close];

}else {
    NSLog(@"DB Close");
}

now you have dictionary with key and you can retrieve value in same order with key. Hope this help you.

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.