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

22 篇文章 0 订阅
20 篇文章 0 订阅


官方文档的描述和回答:

       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使用二进制的数据流,就不会出现此问题.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值