MagicalRecord Deployment and Usage Guide
Prerequisites
- iOS/macOS Development Environment: Xcode 9.0 or later
- Objective-C Knowledge: MagicalRecord is an Objective-C library for Core Data
- Core Data: Basic understanding of Core Data concepts and NSManagedObjectContext
- CocoaPods (Recommended): For easy dependency management in iOS/macOS projects
Installation
Using CocoaPods (Recommended)
-
Add MagicalRecord to your Podfile:
pod 'MagicalRecord' -
Run the following command in your project directory:
pod install -
Open your project using the
.xcworkspacefile generated by CocoaPods.
Manual Installation
-
Clone the MagicalRecord repository:
git clone https://github.com/magicalpanda/MagicalRecord.git -
Add the MagicalRecord source files to your Xcode project:
- Drag and drop the
MagicalRecordfolder into your Xcode project navigator - Ensure "Copy items if needed" is checked
- Add the files to your target
- Drag and drop the
-
Import MagicalRecord in your code:
#import "MagicalRecord.h"
Configuration
Core Data Stack Setup
MagicalRecord requires you to set up your Core Data stack. Here's a basic setup:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"YourDataModel.sqlite"];
return YES;
}
Environment Variables
MagicalRecord doesn't require specific environment variables, but you should configure your Core Data model name and store type based on your application needs.
Build & Run
Development
- Open your Xcode project workspace (
.xcworkspaceif using CocoaPods) - Select your target scheme
- Choose a simulator or connected device
- Click the Run button or press
Cmd+R
Production
- Ensure your Core Data model is properly configured
- Set up proper error handling for Core Data operations
- Consider using background contexts for heavy operations
- Build for distribution using Xcode's archive feature
Deployment
MagicalRecord is a library that runs on iOS and macOS devices, so deployment follows standard iOS/macOS app deployment procedures:
-
App Store Distribution:
- Use Xcode's Archive feature
- Upload to App Store Connect
- Submit for review
-
Enterprise Distribution:
- Use Xcode's Archive feature
- Export as Enterprise build
- Distribute through your enterprise distribution method
-
Ad Hoc Distribution:
- Use Xcode's Archive feature
- Export as Ad Hoc build
- Distribute to registered devices
Usage Examples
Basic Fetching
// Simple fetch
NSArray *people = [Person MR_findAll];
// Fetch with conditions
NSArray *peopleNamedJohn = [Person MR_findByAttribute:@"firstName" withValue:@"John"];
// Fetch with sorting
NSArray *peopleSorted = [Person MR_findAllSortedBy:@"lastName" ascending:YES];
Creating Entities
Person *person = [Person MR_createEntity];
person.firstName = @"John";
person.lastName = @"Doe";
Deleting Entities
[person MR_deleteEntity];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
Saving Context
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
Troubleshooting
Common Issues
-
"NSInvalidArgumentException: Unable to load NSPersistentStoreCoordinator"
- Solution: Ensure your Core Data model name matches what you passed to
setupCoreDataStackWithAutoMigratingSqliteStoreNamed:
- Solution: Ensure your Core Data model name matches what you passed to
-
Performance Issues with Large Datasets
- Solution: Use background contexts with
MR_contextWithParent:and perform operations asynchronously
- Solution: Use background contexts with
-
Data Not Persisting
- Solution: Ensure you're calling
MR_saveToPersistentStoreAndWaiton the appropriate context
- Solution: Ensure you're calling
-
Threading Issues
- Solution: Always use the context associated with the current thread. MagicalRecord provides thread-safe contexts
-
Migration Failures
- Solution: Use
setupCoreDataStackWithAutoMigratingSqliteStoreNamed:for automatic migrations, or implement custom migrations for complex schema changes
- Solution: Use
Debug Logging
Enable MagicalRecord logging for debugging:
[MagicalRecord setLoggingLevel:MagicalRecordLoggingLevelVerbose];
Testing
When writing unit tests, use an in-memory store:
[MagicalRecord setupCoreDataStackWithInMemoryStore];
// Run your tests
[MagicalRecord cleanUp];
Migration Issues
For complex data model changes, consider using custom migrations instead of automatic migrations. Implement NSPersistentStoreCoordinator migration policies for fine-grained control over data transformation during schema changes.