On Feb 26, 2009, at 1:37 PM, James Cicenia wrote:
Here is the code. It works on the simulator:
-(NSMutableArray *)statesWithinMiles:(NSString *)miles{
NSMutableArray *states = [[NSMutableArray alloc]init];
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory
stringByAppendingPathComponent:@"whatsfresh.sql"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
sqlite3_create_function(database, "distance", 4, SQLITE_UTF8,
NULL, &distanceFunc, NULL, NULL);
NSString *tmp = @"SELECT DISTINCT state_abbr as state FROM geoinfo
WHERE distance(Latitude, Longitude, ";
tmp = [tmp stringByAppendingString:[self currentLatitude]];
tmp = [tmp stringByAppendingString:@","];
tmp = [tmp stringByAppendingString:[self currentLongitude]];
tmp = [tmp stringByAppendingString:@") < "];
tmp = [tmp stringByAppendingString:miles];
tmp = [tmp stringByAppendingString:@" and state_abbr NOT NULL
;"];
const char *sql = [tmp
cStringUsingEncoding:NSUTF8StringEncoding];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) ==
SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *str = (char
*)sqlite3_column_text(statement, 0);
[states addObject:(str) ? [NSString stringWithUTF8String:str] :
@""];
}
}
}
return states;
}
Why doesn't this work on the device?
Help this is causing me hours of grief with errors like these.
Your NSMutableArray isn't following Cocoa's memory-management
conventions, so that might be a leak but likely not your "doesn't
work" problem. You're also not cleaning up some of your sql resources
(closing `database`, for example), so that might cause trouble if you
call this multiple times and sqlite3 eventually is unable to open more
files.
Overall, there's a lot of moving parts in that code. You should step
through it with the debugger and look for something you didn't expect
to happen.
"Trust no-one". If you must, "trust but verify".
* Does this method get called at all?
* Are any of these objects nil? Remember that [nil doSomething] is
often a zero-returning no-op in Objective-C, and is never any kind of
null pointer exception.
* Is NSSearchPathForDirectoriesInDomains returning the list you expect?
* Is `documentsDirectory` the path you want?
* Is `path` the value you want? Do you need an extra '/' before
"whatsfresh.sql" ?
* Does sqlite3_open return an error? Do you need to close it when
you're done?
* Does sqlite3_create_function return an error? Do you need to free
distanceFunc?
* Is `sql` the value you want?
* Do any of sqlite3_prepare_v2 or sqlite3_step or sqlite3_column_text
return an error? Are you using these API correctly?
* Does `str` have the value you expect?
* Does `states` have the value you expect just before you return? If
so, the problem may be in the caller of this method, not here.
--
Greg Parker gpar...@apple.com Runtime Wrangler
_______________________________________________
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