<commit_before>//
//  CoreDataAdapter+Private.swift
//  SyncKit
//
//  Created by <NAME> on 04/06/2019.
//  Copyright © 2019 <NAME>. All rights reserved.
//

import Foundation
import CoreData
import CloudKit

typealias IdentifiableManagedObject = NSManagedObject & PrimaryKey

//MARK: - Utilities
extension CoreDataAdapter {
    func savePrivateContext() {
        try? self.privateContext.save()
    }
    
    func configureImportContext() {
        targetImportContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
        targetImportContext.parent = targetContext
    }
    
    func clearImportContext() {
        guard let targetImportContext = targetImportContext else { return }
        targetImportContext.performAndWait {
            self.targetImportContext.reset()
        }
        self.targetImportContext = nil
    }
    
    func deleteAllPendingRelationships() {
        guard let pendingRelationships = try? privateContext.executeFetchRequest(entityName: "QSPendingRelationship") as? [QSPendingRelationship] else { return }
        pendingRelationships.forEach {
            self.privateContext.delete($0)
        }
    }
    
    func deleteInsertedButUnmergedEntities() {
        let pendingEntities = fetchEntities(state: .inserted)
        pendingEntities.forEach {
            $0.entityState = .synced
        }
    }
    
    func updateInsertedEntitiesAndSave() {
        for pending in self.fetchEntities(state: .inserted) {
            pending.entityState = .synced
        }
        savePrivateContext()
    }
    
    func nextStateToSync(after state: SyncedEntityState) -> SyncedEntityState {<commit_msg>Fix for deleteInsertedButUnmergedEntities<commit_after>//
//  CoreDataAdapter+Private.swift
//  SyncKit
//
//  Created by <NAME> on 04/06/2019.
//  Copyright © 2019 <NAME>. All rights reserved.
//

import Foundation
import CoreData
import CloudKit

typealias IdentifiableManagedObject = NSManagedObject & PrimaryKey

//MARK: - Utilities
extension CoreDataAdapter {
    func savePrivateContext() {
        try? self.privateContext.save()
    }
    
    func configureImportContext() {
        targetImportContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
        targetImportContext.parent = targetContext
    }
    
    func clearImportContext() {
        guard let targetImportContext = targetImportContext else { return }
        targetImportContext.performAndWait {
            self.targetImportContext.reset()
        }
        self.targetImportContext = nil
    }
    
    func deleteAllPendingRelationships() {
        guard let pendingRelationships = try? privateContext.executeFetchRequest(entityName: "QSPendingRelationship") as? [QSPendingRelationship] else { return }
        pendingRelationships.forEach {
            self.privateContext.delete($0)
        }
    }
    
    func deleteInsertedButUnmergedEntities() {
        let pendingEntities = fetchEntities(state: .inserted)
        pendingEntities.forEach {
            self.privateContext.delete($0)
        }
    }
    
    func updateInsertedEntitiesAndSave() {
        for pending in self.fetchEntities(state: .inserted) {
            pending.entityState = .synced
        }
        savePrivateContext()
    }
    
    func nextStateToSync(after state: SyncedEntityState) -> SyncedEntityState {
{'fraction_non_alphanumeric': 0.08075089392133493, 'fraction_numerical': 0.00834326579261025, 'mean_word_length': 2.7932203389830508, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5040210', 'n_tokens_mistral': 902, 'n_tokens_neox': 883, 'n_words': 181}