I cache the fields array on the first run so that the method is efficient - 
this is a generic SQLite db wrapper that may potentially be used to access 
1000s of rows, and it seems perfectly reasonable that I should be able to store 
the fields in between SELECTs


On 28 Mar 2010, at 19:04, Jack Carbaugh wrote:

> I'm trying to understand your code logic ...
> 
> It looks as if you are running a query which returns a list of "Fields" ... 
> then the values for each of those fields.
> 
> In the first IF below, you are grabbing the column names to use as keys ... 
> and assigning values for those keys in the 2nd IF.
> 
> The "fields" should be local in scope to THIS METHOD only. Therefore, you 
> should release it after using it.
> 
> if Fields is meant to be used by a tableview or something else, you can get 
> those "keys" from the output dictionary via "allKeys" ...
> 
> so here is my suggested rewrite
> 
> 
> -(NSDictionary *)fetch {
>  NSMutableDictionary *output = [[NSMutableDictionary alloc] init]; // local 
> only to this method, returned autoreleased
> NSMutableArray  *fields = [[NSMutableArray alloc] init];; // local only to 
> this method, only need to grab "column names" from a query
> 
>  if (db != nil && sth != nil) {
>      int c = sqlite3_column_count(sth);
>          for (int i = 0; i < c; ++i) {
>              const char *name = sqlite3_column_name(sth, i);
>              [fields addObject:[NSString stringWithUTF8String:name]];
>          }
>      if (sqlite3_step(sth) == SQLITE_ROW) {
>          for (int i = 0; i < c; ++i) {
>              const unsigned char *val = sqlite3_column_text(sth, i);
>              NSString *value;
>              if (val == nil) {
>                  value = @"{NULL}";
>              }
>              else {
>                  value = [NSString stringWithUTF8String:(char *)val];
>              }
>              [output setObject:value forKey:[fields objectAtIndex:i]];
>          }
>      }
>  }
> // fields no longer needed, so release
> [fields release];
>  return [output autorelease];
> }
> 
>> 
>> However, can anyone answer how I best go about either of the tasks that I 
>> have outlined as red, as they are leaking a lot of memory in 
>> comparison?_______________________________________________
>> 
>> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
>> 
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>> 
>> Help/Unsubscribe/Update your Subscription:
>> http://lists.apple.com/mailman/options/cocoa-dev/intrntmn%40aol.com
>> 
>> This email sent to intrn...@aol.com
> 

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to