如何在iPhone的后台运行sqlite? [英] How run sqlite in background on the iPhone?

查看:92
本文介绍了如何在iPhone的后台运行sqlite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何按照这是我的尝试:

- (void) run:(NSString *)sql {
NSArray *data = [NSArray arrayWithObjects:
                 sql,
                 [self returnItemClass],
                 nil];

NSInvocationOperation *operation = 
[[NSInvocationOperation alloc] initWithTarget:self 
                                     selector:@selector(loadRecords:) 
                                       object:data]; 
[self.queue addOperation:operation];
[operation release];
}

- (void) loadRecords:(NSArray *)data {
    NSLog(@"Runing sql");
    NSString * sql = [data objectAtIndex:0];
    Class cls = [data objectAtIndex:1];

    Db *db= [Db currentDb];

    NSArray *list = [db loadAndFill:sql theClass:cls];
    [UIAppDelegate performSelectorOnMainThread:@selector(recordsLoaded:)
                                           withObject:list
                                        waitUntilDone:YES];
}

- (void) recordsLoaded:(NSArray *)data {
NSLog(@"Loading sql");
for (DbObject *o in data) {
    [self.objectCache setObject:o forKey:[NSNumber numberWithInt:o.Id]];
}
}

但是,尝试运行时出现错误

However, I get a error when try to run

    [UIAppDelegate performSelectorOnMainThread:@selector(recordsLoaded:)
                                       withObject:list
                                    waitUntilDone:YES];

抱怨无法将消息发送到recordsLoaded.

And complain about not can send a message to recordsLoaded.

这是正确的方法吗?

以及如何在UITableView中填充数据?

And how fill the data in the UITableView?

推荐答案

由于Apple的意愿和设计,在多线程iPhone应用程序中的方式没有太多.

There isn't too much in the way of multi-threading iPhone applications, as a result of Apple's wishes and design.

但是,通用消息传递原理有助于编写面向事件的程序.您的程序应旨在通过将事件代理程序的代表设置为响应用户输入和环境变量.

The generic messaging philosophy, however, is conducive to writing event-oriented programs. Your program should aim to respond to user input and environmental variables by setting the delegates to event dispatchers.

SQLite3作为数据库,不是传统意义上的服务器.它与特定文件格式进行交互的协议,其文件库可以直接包含在源代码中.换句话说,当您需要从数据库中获取数据时,您应该能够立即读取它.您不应该也不能产生一个在后台工作的SQLite3进程.

SQLite3, as a database, is not a server in the classical sense. Its protocol for interacting with a specific file format, whose libraries can be directly included in source code. In other words, when you need to get data from the database, you should be able to read it immediately. You shouldn't, and can't, spawn a SQLite3 process to work in the background.

关于所声明的语法,为什么使用performSelectorOnMainThread...而不是仅仅调用

Regarding your stated syntax, why are you using performSelectorOnMainThread... instead of just calling

[UIAppDelegate recordsLoaded:list]

?

这篇关于如何在iPhone的后台运行sqlite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆