IOS工程师面试被问到的问题SQLite store takes a "long time" to save怎么解决?

来源:互联网 发布:淘宝网店流量数据分析 编辑:程序博客网 时间:2024/06/07 08:01


官方文档的描述和回答:

       Problem: You are using an SQLite store and notice that it takeslonger to save to the SQLite store than it does to save the same data to an XMLstore.

Cause: This is probably expected behavior. The SQLite store ensures that all data is written correctly to disk—see“Configuringa SQLite Store’s Save Behavior.”

       Remedy: First determine whether the time taken to save will benoticeable to the user. This is typically likely to be the case only if youconfigure your application to frequently save automatically—for example, afterevery edit that the user makes. First, consider changing the store’s save behavior (switch off full sync). Then consider saving data only after a setperiod (for example, every 15 seconds) instead of after every edit. Ifnecessary, consider choosing a different store—for example, the binary store.

 

解决卡顿的方法:

1 关闭fullfsync,然后调整频率

NSPersistentStoreCoordinator *psc = <#Get a persistent store coordinator#>;

 

 

 

NSMutableDictionary *pragmaOptions = [NSMutableDictionary dictionary];

 

[pragmaOptions setObject:@"NORMAL" forKey:@"synchronous"];

 

[pragmaOptions setObject:@"1" forKey:@"fullfsync"];

 

 

 

NSDictionary *storeOptions =

 

    [NSDictionary dictionaryWithObject:pragmaOptions forKey:NSSQLitePragmasOption];

 

NSPersistentStore *store;

 

NSError *error = nil;

 

store = [psc addPersistentStoreWithType:NSSQLiteStoreType

 

            configuration: nil

 

            URL:url

 

            options:storeOptions

 

            error:&error];

2如果还不满意,自己去手工每15秒调用保存(卡顿)方法,算是负载均衡(不知是否用多线程)

3使用二进制的数据流,就不会出现此问题.

原创粉丝点击