本文介绍了如何在iPhone上使用带有线程的sqlite + fdbm库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与相关,我想将数据加载到后台。

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

然而,我得到'库例程称为无序'错误。

However, I get 'library routine called out of sequence' errors.

在说方式是使用NSOperation,但查看样本上的web我不知道如何解决这个问题。

In this SO thread say that the way is using NSOperation, but looking on the samples on the web I not know how that could solve the issue.

我与单例模式共享一个sqlite连接:

I share a single sqlite connection with the 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 is harder open twice the same db....

任何想法?

编辑:

我确认错误是在调用sqlite。我使用FDBM作为瘦包装来调用它。

I confirmed the error is in calling sqlite. I use FDBM as thin wrapper for calling it.

我正在运行2个线程:主要和后台任务,用于加载数据。我这样运行:

I'm running 2 threads: the main and a background task for loading of the data. I run it this way:

- (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接口的代码位于 - 特别是Db.h / m,它是与fdbm / sqlite接口的唯一单元。

The code for the db interface is at http://code.google.com/p/chibiorm/source/browse/#svn/trunk/src — specifically the Db.h/m that are the only units that interface with fdbm/sqlite.

尝试调用sqlite时发生错误来自FDBM的函数。

The error happend when try to call sqlite functions from FDBM.

例如在这里发生:

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

这次调用FDBM代码:

This call the FDBM code:

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


推荐答案

我终于找到了一个有效的解决方案。

I finally found a working solution.

这个想法是构建一个连接数据库池并打开两次db。使用一个连接作为主线程,另一个连接作为后台。

The idea is build a database pool of connection and open twice the db. Use one connection for the main thread and the another for the background.

现在所有内容都在

这篇关于如何在iPhone上使用带有线程的sqlite + fdbm库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 01:30
查看更多