CoreData线程安全对象上下文保存

时间:2015-10-03 12:48:11

标签: swift core-data

我有一个使用获取的结果控制器填充的tableview。它会在数据发生变化时自动更新。

我还有一个后台线程处理文件,当我触发保存时,我希望这个tableview更新。目前我遇到了一些奇怪的错误,我不确定这是否是正确的设置方法。

我将appdelegate中的主要managedObjectContext修改为主队列类型:

<div class="container">
      
      <form> 
        <span class="sub-head">material:</span>
        <input id="mat-paper" type="radio" name="material" value="paper">
        <label for="mat-paper">paper</label>  
        <input id="mat-plastic" type="radio" name="material" value="plastic">
        <label for="mat-plastic">plastic</label><br/>
    
        <span class="sub-head">shape:</span>
        <input id="shape-round" type="radio" name="shape" value="round">
        <label for="shape-round">round</label>
        <input id="shape-squere" type="radio" name="shape" value="squere">
        <label for="shape-squere">squere</label>  <br/>
    
        <span class="sub-head">size:</span>
        <input type="radio" name="size" value="S">
        <label for="size-s">S</label>
        <input type="radio" name="size" value="L">
        <label for="size-l">L</label>
        <input type="radio" name="size" value="M">
        <label for="size-m">M</label>
        <input type="radio" name="size" value="XL">  
        <label for="size-xl">XL</label><br><br>
        <input type="reset" value="Reset!"><br><br>
      </form>
      
    <p>TEST QUERIES:</p>  
    <p>"paper, round, xl" should show only: PRODUCT 6</p> 
    <p>"plastic, round, xl" should show: PRODUCT 2,3,5 </p> 
      
      
    	<div data-categories='["paper", "plastic", "round", "squere", "s"]' class="item">PRODUCT 1</div>
    	<div data-categories='["plastic", "round", "squere", "s", "m","l","xl"]' class="item">PRODUCT 2</div>
    	<div data-categories='["plastic", "round", "squere", "l","xl"]' class="item">PRODUCT 3</div>
    	<div data-categories='["paper", "round", "s"]' class="item">PRODUCT 4</div>
    	<div data-categories='["plastic", "round", "xl"]' class="item">PRODUCT 5</div>
    	<div data-categories='["paper", "round", "xl"]' class="item">PRODUCT 6</div>
    
    <div class="item sorry">SORRY</div>	
    </div>

然后在我的线程中,我正在创建私有队列类型:

lazy var managedObjectContext: NSManagedObjectContext? = {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
    let coordinator = self.persistentStoreCoordinator
    if coordinator == nil {
        return nil
    }
    var managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
}()

然后我会在我的帖子中稍后进行保存:

let mainManagedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let privateManagedObjectContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
privateManagedObjectContext!.parentContext = mainManagedObjectContext

这确实有用..但是它不是持久性的,我想我需要在managedObjectContext上进行保存。这是对的吗?

更新

它现在正在工作..我只是没有保存到主要上下文这里是我的代码,如果有人卡住:

 do {
      try self.privateManagedObjectContext!.save()
 } catch {
 }

0 个答案:

没有答案
相关问题