-2

So I have a class, that at this point only contains a simple NSMutableArray object. I'm setting this object from a view controller but for some reason... the silly thing doesn't work on my ipad. It works flawlessly on the simulator however.

Has anyone ever come across this scenario?

I know that's not a whole lot to go on but any tips/pointers on where to look would even be helpful. No idea what to search for on this one.

Thanks


ViewController.h

#import "DBEntries.h"
DBEntries *dbEntries;

ViewController.m:

[database open];

NSMutableArray *arrayAdder = [[NSMutableArray alloc] init];

NSString *filterNameFirst = [NSString stringWithFormat:@"%@", nameFirstTextField.text];
NSString *filterNameLast = [NSString stringWithFormat:@"%@", nameLastTextField.text];

NSString *filterStringFirst = [NSString stringWithFormat:@"%%%@%%", filterNameFirst]; // Works on first name only.
NSString *filterStringLast = [NSString stringWithFormat:@"%%%@%%", filterNameLast]; // Works on last name only.

FMResultSet *results = [database executeQuery:[NSString stringWithFormat:@"SELECT * FROM users WHERE firstname LIKE ? AND lastname LIKE ?", filterStringFirst, filterStringLast]];

while([results next])
{
    NSString *firstname = [results stringForColumn:@"firstname"];
    NSString *lastname = [results stringForColumn:@"lastname"];

    // Add db entries to array in DBEntries.
    NSString *objectAdder = [NSString stringWithFormat:@"%@ %@", firstname, lastname];
    NSLog(@"objectAdder: %@", objectAdder);
    [arrayAdder addObject:objectAdder];
}

[dbEntries setUsersFiltered:arrayAdder];
[arrayAdder release];

[database close];

isUpdatingEntriesForTableView = YES;

[dbTableView reloadData];

NSLog(@"dbEntries.usersFiltered: %@", dbEntries.usersFiltered); // This logs 0 on device, but works on simulator.

class.h:

NSMutableArray *usersFiltered;
@property (strong, nonatomic) NSMutableArray *usersFiltered;

class.m:

@synthesize usersFiltered;
crewshin
  • 765
  • 9
  • 22
  • 6
    Please post some related code so we can identify what might be going wrong. – Mick MacCallum Aug 05 '12 at 01:40
  • As you said yourself, there's nothing here for anyone to help you without code and the error you are seeing. – Firoze Lafeer Aug 05 '12 at 01:44
  • The concept is what I was referring to... but ok, I updated the main post with some code. There are no errors or anything. It just doesn't update the array. So the usersFiltered array returns 0. It's a really basic setup, I was just curious if anyone has run into something similar where class variables wont get set on the device, but WILL on the simulator. – crewshin Aug 05 '12 at 04:30
  • This is just a guess, but try looking at where you save your SQlite file which contains the data. Try deleting the app from the simulator and install it back, if it still works then it's not the SQLite problem. Try doing the same on your iPad. Again, as of right now, this is my best guess. Could you post more code on your core data model? – sridvijay Aug 05 '12 at 04:32
  • Posted more code. Sqlite db using FMDB libraries. But that stuff is kind of irrelevant I think. – crewshin Aug 05 '12 at 04:46
  • 1
    What exactly doesn't work? Does it crash, is the array empty? Also, how are you opening your database. I have the feeling that this has very much to do with your problem. Btw, your query looks weird, you're using `?` as placeholders, but that doesn't have any effect with `stringWithFormat:`. I'd be surprised if that query works anywhere the way you expect it to. – omz Aug 05 '12 at 04:50
  • Perform some more logging, particularly during your while loop. There is no way this is a problem with NSMutableArray. Your database will not be opening on the device, probably because you have mixed up the case of a filename somewhere (device is case sensitive, simulator is not). – jrturton Aug 05 '12 at 05:34
  • @omz Nothing crashes... it just logs 0. As if the array was never set. Again, let me repeat that it works perfectly on the simulator but NOT on my device (iPad3). – crewshin Aug 05 '12 at 17:14
  • @jrturton That's what I have been thinking too. I just don't get how this isn't working. Btw, this has nothing to do with any db. It's purely an NSMutableArray. It all logs perfectly in the while loop too. Before and after the `[arrayAdder addObject:objectAdder];`. Good info about case sensitivity btw. Thanks for that! – crewshin Aug 05 '12 at 17:19
  • @omz yeah I was confused by that too... but for some reason it seems to work even though it chokes out a warning. Your reply to this post [http://stackoverflow.com/questions/9008682/fmdb-query-isnt-acting-right-with-like] is what got me going in that direction btw. – crewshin Aug 05 '12 at 17:29
  • You misunderstood my answer then. The `?` are SQL placeholders, just remove the `stringWithFormat:` and use the parameters directly in `executeQuery:`, like this: `FMResultSet *results = [database executeQuery:@"SELECT * FROM users WHERE firstname LIKE ? AND lastname LIKE ?", filterStringFirst, filterStringLast];` – omz Aug 05 '12 at 17:34
  • Yeah I just noticed what I did wrong and it fixed the issue. Thanks man. – crewshin Aug 05 '12 at 17:50

2 Answers2

1

I don't use strong for automatic memory counting i just don't like it. Have you tried

@property (retain, nonatomic) NSMutableArray *usersFiltered;

and you can replace the following lines:

[dbEntries setUsersFiltered:arrayAdder];
[arrayAdder release];

with:

dbEntries.usersFiltered = [arrayAdder autorelease];
//[arrayAdder release];
OnlyAngel
  • 157
  • 1
  • 7
0

Wow. Ok so the answer was related to @omz 's point. I was using [NSString stringWithFormat when I shouldn't have been. Removing that so it's FMResultSet *results = [database executeQuery:@"SELECT * FROM users WHERE firstname LIKE ? AND lastname LIKE ?", filterStringFirst, filterStringLast]; now doesn't fire the warning and works on the device. Funny that it worked on the simulator but not device before however.

crewshin
  • 765
  • 9
  • 22