How to use sqlite + fdbm library with streams on iPhone

Related to this SO question , I want to put data in the background.

However, I get library errors called from a sequence.

In this thread, SO says that the method uses NSOperation, but looking at samples on the Internet, I do not know how this could solve the problem.

I use one sqlite connection with a singleton pattern:

@interface Db : NSObject {
    NSString *path;
    FMDatabase* theDb;
    BOOL isOpen;
}

@property (retain, nonatomic) FMDatabase *theDb;
@property (retain, nonatomic) NSString *path;
@property (nonatomic) BOOL isOpen;
--------
static Db *currentDbSingleton = nil;
#pragma mark Global access

+(id)currentDb {
    @synchronized(self) {
        if (!currentDbSingleton) {
            NSString *reason = NSLocalizedString(@"The database is not set globally",
                                                 @"Error Db: database is not set");
            NSException *e = [NSException exceptionWithName:@"DBError"                        
                                                     reason:reason;
                                                   userInfo:nil];
            @throw e;
        }
    }
    return currentDbSingleton;  
}

So it’s more difficult to open the same db twice ....

Any ideas?

EDIT:

I have confirmed that the error lies in calling sqlite. I use FDBM as a thin shell to invoke it.

2 flows work for me: the main and background task for loading data. I run it like this:

- (void) fillCache:(NSString *)theTable {
    [NSThread detachNewThreadSelector:@selector(fillCacheBackground:)
                             toTarget:self
                           withObject:theTable];
}

- (void)loadComplete {
    [self.table reloadData];
}

- (void) fillCacheBackground:(NSString *)theTable {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    Db *db= [Db currentDb];
    [db beginTransaction];
        ..... STUFF HERE
    [db commitTransaction];
    //Tell our callback what we've done
    [self performSelectorOnMainThread:@selector(loadComplete) 
                           withObject:nil 
                        waitUntilDone:YES];
    [pool drain];
}

db http://code.google.com/p/chibiorm/source/browse/#svn/trunk/src - , Db.h/m, , fdbm/SQLite.

sqlite FDBM.

, :

-(void) checkError {
    if ([self.theDb hadError]) { // <====ERROR HERE
        NSLog(@"Err %d: %@", [self.theDb lastErrorCode], [self.theDb]);
    }
}

FDBM:

- (BOOL) hadError {
    int lastErrCode = sqlite3_errcode(db);
    return (lastErrCode > SQLITE_OK && lastErrCode < SQLITE_ROW);
}
+3
2
+2

singleton - , , currentDbSingleton ... , , , , .

, , , , (SQLite FMDB) , / . , concurrency, , , " ", , . , , .

NSOperation. " " - , . , , NSOperation.


: ( , )

checkError, .m, , hadError . ( ? , ?)

, , fillCache , ? . , - beginTransaction inTransaction is YES, - ( FMDatabase ivar). , , , . ( FMDatabase , beginTransaction .)

, Apple Objective-C. NSLock ( lockBeforeDate:) . -[Db beginTransaction] , , .

, + allocWithZone: - + inizialize ( , ), , . ( , + alloc, -initWithName:, + setCurrentDb. , + initializeWithPath: , .)

gotchas, , + setCurrentDb: singleton , ( ) + currentDb , , , singleton .. , , - concurrency. , FMDatabase , X NSOperation . , theDb , , . , , .

: TypeForField:Type: ValueForField:Name:Type: typeForFieldName:typeName: valueForResultSet:fieldName:typeName: . , .

+3

Source: https://habr.com/ru/post/1711882/


All Articles