Question

IM USING SQLITE DATABASE for my iPhone app

In my app in data base to retried the content from the database i used an array and i return this array in a database method for select statement,

for this purpose i allocate an array and i need to release the array,

NSMutableArray *allContacts = [[NSMutableArray alloc] init];
 return allContacts;

if i release an array how sh'd i used in return statement

viceversa if i tried to release after return (we can do anything after return)

Any solution please....

How should we use auto release NSMutable array

//Select statement for contacts
//==================================
+ (NSMutableArray*) selectAllContactsFromDB
{
      NSString *DBPath = [self copyDBFile];
      sqlite3 *contactsDB = nil;
      sqlite3_stmt *statement = nil;
      NSMutableArray *allContacts = [[NSMutableArray alloc] init];

      if (sqlite3_open([DBPath UTF8String], &contactsDB) == SQLITE_OK)
      {
            NSString *query = [NSString stringWithFormat: @"SELECT ROWID, NAME, MOBILE,  FROM CONTACTINFO"];
            if(sqlite3_prepare_v2(contactsDB, [query UTF8String], -1, &statement, NULL) == SQLITE_OK)
            {
                  while (sqlite3_step(statement) == SQLITE_ROW)
                  {
                        Contact *contact = [[Contact alloc] init];
                        contact.rowid = sqlite3_column_int(statement, 0);
                        contact.name = [NSString stringWithUTF8String:(const char*) sqlite3_column_text(statement, 1)];
                        contact.mobile = [NSString stringWithUTF8String:(const char*) sqlite3_column_text(statement, 2)];


                        [allContacts addObject:contact];
                  }
            }
            else {
                  NSLog(@"Statement not prepared");
            }
      }
      [DBPath release];
      return allContacts;
}
Était-ce utile?

La solution

When you return an allocated object from a method pass it as an autoreleased object.

return [allContacts autorelease];

When you get an autoreleased object you need to retain it for further use:

So change the calling method like;

NSMutableArray *temp = [[YourClass selectAllContactsFromDB] retain];

Autres conseils

try like this

NSMutableArray *allContacts = [[[NSMutableArray alloc] init] autorelease];

and also like this..

return [allContacts autorelease];

You can write return statement return [allContacts autorelease]; OR you can use ARC in your project.

You will have to use autorelease :

return [allContacts autorelease];

This way il will be released the next time the autorelease pool will be flushed. And you have followed the golden rule : For each alloc, copy or retain, you must have a release or autorelease.

use ARC(Automatic Reference Counting) or you have a property as a mutuable array and just return the array...

to get u started on ARC, watch this: http://www.youtube.com/watch?v=FxuI4e_Bj58

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top