No description or website provided.
Swift
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
CoreStore.xcodeproj
CoreStore
CoreStoreTests
CoreStoreUITests
.gitignore
CoreDataModel.png
LICENSE.txt
README.md

README.md

LearnCoreData

This project aims at providing a light-weight, easy-to-understand, and robust solution for applications which tend to use Core Data.

How to use?

Define Your Model

The models we intend to store in Core Data in this project.

Contact {

  firstName:  String?
  lastName:   String?
  country:    Country
}

Country {

  name: String?
}

Create your model

alt tag

Implement the required protocols

  1. protocol ResourceMetaInfo
  2. protocol CoreStorable

In the case of our Contact we would have:

struct ContactMetaInfo: ResourceMetaInfo {

    /// The name of the momd file.
    var coreStoreModelName      : String { return "Contact" }

    /// The name of the sqlite file.
    var coreStoreSQLiteFilename : String {

        if isRunningTests() {

            return "\(coreStoreModelName)Test.sqlite"
        } else {

            return "Contacts.sqlite"
        }
    }

    /// The name of the entity in the model file.
    var entityName: String {

        return "Contact"
    }
}



extension Contact: CoreStorable {

    static var metaInfo: ResourceMetaInfo {

        return ContactMetaInfo()
    }

    static var fetchRequestWithDescriptor: NSFetchRequest<Contact> {

        let request = NSFetchRequest<Contact>(entityName: "Contact")

        let departmentSort = NSSortDescriptor(key: "firstName", ascending: true)
        let lastNameSort = NSSortDescriptor(key: "lastName", ascending: true)
        request.sortDescriptors = [departmentSort, lastNameSort]

        return request
    }
}

Use Data Providers in your view controllers

If you have a UI operation on data such as intention to show information in a collection view then use the UI operation mode. This create a a context that is on the main thread suitable for UI operations.

let dataProvider = DataProvider<Contact>(operationMode: .UI)

If you need the data provider for Data operation, then you probably need a managed context where has its own operation queue.

let dataProvider = DataProvider<Contact>(operationMode: .Data)

Insertion

dataProvider.insert(beforeSave: { (beforeSaveContact) -> Bool in

    guard let validContact = beforeSaveContact else { return false }

    validContact.firstName = firstName
    validContact.lastName = lastName

    return true
}, completion: { (savedContact) in

    // Use it
})

Fetch

dataProvider.fetch(fetchRequest: Contact.fetchAllRequest, completion: { contacts in
  // you have fetched contacts
})

Update

For this lets insert a new object and then update some values. we use the context embedded in each object model

dataProvider.insert(beforeSave: { (beforeSaveContact) -> Bool in

  guard let validContact = beforeSaveContact else { return false }

  validContact.firstName = firstName
  validContact.lastName = lastName

  return true
}, completion: { (savedContact) in

  // Update values
  savedContact?.firstName = "New Name"
  try? savedContact?.managedObjectContext.save()
})

Delete

let contactTobeDeleted: NSManagedObject = ...
dataProvider.delete(item: contactTobeDeleted, completion: { (success) in
    // handle success
})

Sync data with table views

Add a fetchWithTable: FetchResultsControllerWithTableView property to your view controller and initiate the sync process with the following command.

func viewDidLoad() {

  super.viewDidLoad()
  self.fetchWithTable = FetchResultsControllerWithTableView(tableView: contactsTableview, fetchController: dataProvider.fetchResultController)
}