## Pear CoreDataManager
Pear CoreDataManager is a lightweight and efficient iOS library for managing Core Data, providing an intuitive and simplified interface for common tasks such as data fetching, inserting, updating, and deleting.
### Features
– Simplified Core Data management
– Convenient data fetching
– Efficient data insertion, updating, and deletion
– Automatic handling of background contexts
– Thread-safe operations
– Support for multiple persistent stores
– Efficient batch processing
### Requirements
– iOS 10.0+
– Swift 5.0 or later
– Xcode 11 or later
### Installation
To integrate Pear CoreDataManager into your iOS project, you can use CocoaPods or manually add the library to your project.
#### CocoaPods
1. Open your **Podfile**.
2. Add the following line to your target:
“`ruby
pod ‘PearCoreDataManager’
“`
3. Run `pod install` in the terminal.
4. Import the library in your Swift files:
“`swift
import PearCoreDataManager
“`
#### Manual Installation
1. Download the latest release from the [Pear CoreDataManager repository](https://github.com/PearCoding/PearCoreDataManager).
2. Drag the **PearCoreDataManager.xcodeproj** file into your Xcode project.
3. In your Xcode project, select your target and go to **Build Phases**.
4. Expand the **Depends on (targets)** section and click on the “+” button.
5. Select **PearCoreDataManager** from the list. This ensures the library is linked to your target.
6. Import the library in your Swift files:
“`swift
import PearCoreDataManager
“`
### Usage
To start using Pear CoreDataManager in your iOS app, follow these steps:
#### 1. Core Data Stack Setup
In your application’s **AppDelegate**, inside the `func application(_:didFinishLaunchingWithOptions:) -> Bool` method, initialize the Core Data stack using the following code:
“`swift
PearCoreDataManager.shared.setup(modelName: “YourDataModel”, storeType: .sqlite)
“`
Replace `”YourDataModel”` with the name of your Core Data model file located in your Xcode project.
#### 2. Fetching Data
To fetch data from your Core Data entities, use the `fetch` method of `PearCoreDataManager.shared`, passing in the entity name and optional fetch request parameters. Here’s an example:
“`swift
PearCoreDataManager.shared.fetch(entityName: “User”) { (users: [User]) in
// Perform operations with the fetched users array
}
“`
#### 3. Inserting Data
To insert new data into your Core Data entities, create a new instance of the entity using the managed object context provided by Pear CoreDataManager. Here’s an example:
“`swift
let newUser = User(context: PearCoreDataManager.shared.managedObjectContext)
newUser.name = “John Doe”
newUser.age = 28
// Save changes to the context
PearCoreDataManager.shared.saveContext()
“`
#### 4. Updating Data
To update existing data in your Core Data entities, fetch the desired object and modify its properties. Here’s an example:
“`swift
if let user = PearCoreDataManager.shared.fetchSingle(entityName: “User”, predicate: NSPredicate(format: “name == %@”, “John Doe”)) as? User {
user.age = 30
// Save changes to the context
PearCoreDataManager.shared.saveContext()
}
“`
#### 5. Deleting Data
To delete data from your Core Data entities, fetch the desired object and delete it from the managed object context. Here’s an example:
“`swift
if let user = PearCoreDataManager.shared.fetchSingle(entityName: “User”, predicate: NSPredicate(format: “name == %@”, “John Doe”)) as? User {
PearCoreDataManager.shared.delete(entity: user)
// Save changes to the context
PearCoreDataManager.shared.saveContext()
}
“`
#### 6. Multithreading
Pear CoreDataManager automatically handles the creation of background managed object contexts for you. This allows you to safely perform Core Data operations on background threads, avoiding thread conflicts and improving performance. Here’s an example:
“`swift
PearCoreDataManager.shared.performBackgroundTask { (context) in
// Perform time-consuming data operations in the background context
// Save changes to the background context
PearCoreDataManager.shared.saveContext(context: context)
}
“`
#### 7. Multiple Persistent Stores
If you need to work with multiple persistent stores, you can add them during the setup phase. Here’s an example:
“`swift
PearCoreDataManager.shared.setup(modelName: “YourDataModel”, storeType: .sqlite)
PearCoreDataManager.shared.addPersistentStore(name: “AdditionalStore”, storeType: .binary)
“`
Replace `”YourDataModel”` with the name of your Core Data model file, and `”AdditionalStore”` with the desired name for the additional persistent store.
### Conclusion
Pear CoreDataManager provides a convenient and efficient way to manage Core Data in your iOS app, simplifying common operations and ensuring thread safety. Take advantage of its features and leverage the power of Core Data in a hassle-free manner.