关于Core Data何时commit的提问 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
iYu
V2EX    iDev

关于Core Data何时commit的提问

  •  
  •   iYu
    iiiyu 2012-08-29 19:59:11 +08:00 4140 次点击
    这是一个创建于 4795 天前的主题,其中的信息可能已经有所发展或是发生改变。
    这两天看core data。然后试着做。出现下列问题。就是我insert进去一个数据。core data并没有马上提交到数据库里面。类似缓存了一下。然后 大概过15秒才commit。这时候 如果我退出app。那数据就丢失了。
    找了一下,并没有找到解决方法。
    类似这样
    http://stackoverflow.com/questions/11795391/core-data-commit-notification

    请问大家是怎么解决这个问题的。我使用的是斯坦福老头的UIManagedDocument方法。并没有直接Sqlite。
    7 条回复    1970-01-01 08:00:00 +08:00
    jjgod
        1
    jjgod  
       2012-08-29 20:11:55 +08:00
    你为啥不直接调用 NSManagedObjectContext 的 -save:?
    iYu
        2
    iYu  
    OP
       2012-08-29 20:12:31 +08:00
    @jjgod save了一样等15秒
    jjgod
        3
    jjgod  
       2012-08-29 20:14:48 +08:00
    在 Mac OS X 下是即时的,不知道 iOS 玩了什么花样。
    iYu
        4
    iYu  
    OP
       2012-08-29 20:17:40 +08:00



    @jjgod 贴两张图。
    iYu
        5
    iYu  
    OP
       2012-08-29 21:36:39 +08:00
    无耻的最后顶一次
    everbird
        6
    everbird  
       2012-08-30 10:15:13 +08:00   1
    线索一:http://stackoverflow.com/questions/9789697/why-does-ios-delay-when-saving-core-data-via-a-uimanageddocument
    线索二:http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreData/Articles/cdTroubleshooting.html 的Problems with Saving部分第一条SQLite store takes a "long time" to save

    只是“不知google之”的结果,未实践,仅供参考
    iYu
        7
    iYu  
    OP
       2012-08-30 13:24:24 +08:00
    @everbird
    @jjgod 昨天弄了一晚上。。首先发现是我测试的问题。因为我是insert以后 就直接Cmd-R来重新运行程序。所以,不是正常退出程序。发现这个问题以后。

    目前 解决方法
    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [self saveContext];
    }
    - (void)applicationWillTerminate:(UIApplication *)application
    {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [self saveContext];
    }


    - (void)saveContext
    {

    OMPCoreDataManager *dataManager = [OMPCoreDataManager shareOMPCoreDataManager];
    NSManagedObjectContext *managedObjectCOntext= dataManager.ompDatabase.managedObjectContext;
    if (managedObjectContext != nil) {
    [managedObjectContext performBlock:^{
    NSError *error = nil;
    if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
    // Replace this implementation with code to handle the error appropriately.
    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
    }
    }];

    }
    }
    用了类似这样的解决方法。看stackoverflow上说并不能保证100%的正确性。不够我测试了一下真机上 应该没有问题。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5218 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 09:13 PVG 17:13 LAX 02:13 JFK 05:13
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86