-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPCoreDataStackManager.h
More file actions
128 lines (90 loc) · 5.1 KB
/
APCoreDataStackManager.h
File metadata and controls
128 lines (90 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// APCoreDataStackManager.h
// APCoreDataStackManager
//
// Created by Axel Péju on 14/11/11.
// Copyright (c) 2011 Axel Péju.
//
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@protocol APCoreDataStackManagerDelegate;
@interface APCoreDataStackManager : NSObject
// Root managed object context
@property (nonatomic, readonly, strong) NSManagedObjectContext * rootManagedObjectContext;
// Persistent store state
@property (nonatomic, readonly, getter = isPersistentStoreUbiquitous) BOOL persistentStoreUbiquitous;
// Delegate
@property (assign) id <APCoreDataStackManagerDelegate> delegate;
// ---------------------------
// Initialization
// ---------------------------
- (id)initWithUbiquityIdentifier:(NSString *)ubiquityIdentifier persistentStoreFileName:(NSString *)storeFileName modelURL:(NSURL *)modelURL;
// ---------------------------
// iCloud availabilty
// ---------------------------
- (void)checkUbiquitousStorageAvailability;
// ---------------------------
// Persistent store management
// ---------------------------
// Resets the Core Data stack and sets it up with a local or ubiquitous persistent store depending on the delegate's result for coreDataStackManagerShouldUseUbiquitousStore:
- (void)resetStackWithAppropriatePersistentStore:(void(^)(NSManagedObjectContext *, NSError *))completionHandler;
// Switches to the local persitent store. If the current store is local, this does nothing.
// If there is no local store file, a new store is created.
- (void)resetStackToBeLocalWithCompletionHandler:(void (^)(NSManagedObjectContext *, NSError *))completionHandler;
// Resets the Core Data stack and sets it up to be ubiquitous.
// If there is no existing ubiquitous persistent store, this method does nothing.
- (void)resetStackToBeUbiquitousWithCompletionHandler:(void (^)(NSManagedObjectContext *, NSError *))completionHandler;
// Resets the Core Data stack and uses the persistent store.
- (void)replaceLocalPersistentStoreWithStoreAtURL:(NSURL *)originStoreURL completionHandler:(void(^)(NSManagedObjectContext *, NSError *))completionHandler;
// Resets the Core Data stack and seeds the store at originStoreURL as initial content
- (void)replaceCloudStoreWithStoreAtURL:(NSURL *)originStoreURL completionHandler:(void (^)(NSManagedObjectContext *, NSError *))completionHandler;
// ----------------------
// Persistent stores URLs
// ----------------------
// Returns the URL of the currently used persistent store. Returns nil if no store is currently used (during a Core Data Stack reset).
- (NSURL *)currentStoreURL;
// Returns the URL of the ubiquitous persistent store. This will return a valid URL if the ubiquitous storage is enabled, and a store has already been seeded.
- (NSURL *)ubiquitousStoreURL;
// Returns the local store URL.
- (NSURL *)localStoreURL;
// Persistent store data
- (NSData *)persistentStoreData;
// URLs of previously persistent stores in the local ubiquitous container
- (NSArray *)previouslyUsedPersistentStoresURLs;
@end
@protocol APCoreDataStackManagerDelegate <NSObject>
@required
// Requests the delegate to refresh the stack using the local store
- (void)coreDataStackManagerRequestLocalStoreRefresh:(APCoreDataStackManager *)manager;
// Requests the delegate to refresh the stack using the ubiquitous store
- (void)coreDataStackManagerRequestUbiquitousStoreRefresh:(APCoreDataStackManager *)manager;
@optional
// Request the delegate to handle the migration
- (void)coreDataStackManager:(APCoreDataStackManager *)manager
migrateStoreAtURL:(NSURL *)storeURL
withDestinationManagedObjectModel:(NSManagedObjectModel *)model
completionHandler:(void (^)(BOOL, NSError *))completionHandler;
- (BOOL)coreDataStackManagerShouldUseUbiquitousStore:(APCoreDataStackManager *)manager;
- (void)coreDataStackManagerWillAddUbiquitousStore:(APCoreDataStackManager *)manager;
- (void)coreDataStackManagerDidAddUbiquitousStore:(APCoreDataStackManager *)manager;
- (void)coreDataStackManagerWillAddLocalStore:(APCoreDataStackManager *)manager;
- (void)coreDataStackManagerDidAddLocalStore:(APCoreDataStackManager *)manager;
- (void)coreDataStackManagerWillReplaceUbiquitousStore:(APCoreDataStackManager *)manager;
- (void)coreDataStackManagerDidReplaceUbiquitousStore:(APCoreDataStackManager *)manager;
// URL of the application's documents director
- (NSURL *)coreDataStackManagerApplicationDocumentsDirectory:(APCoreDataStackManager *)manager;
@end
// Errors
#define CORE_DATA_STACK_MANAGER_ERROR_DOMAIN @"APCoreDataStackManagerErrorDomain"
enum
{
APCoreDataStackManagerErrorUnknown = -1,
APCoreDataStackManagerErrorDocumentStorageUnavailable = 100,
APCoreDataStackManagerErrorDocumentStorageAvailabilityTimeOut = 101,
APCoreDataStackManagerErrorNoUbiquitousPersistentStoreFound = 102,
APCoreDataStackManagerErrorLocalStoreURLUnavailable = 200,
APCoreDataStackManagerErrorFileFoundAtApplicationDirectoryURL = 300,
};
// Notifications
#define APPERSISTENTSTOREDIDCHANGENOTIFICATION @"APPersistentStoreDidChangeNotification"
#define APUBIQUITOUSSTORAGEAVAILABILITYDIDCHANGENOTIFICATION @"APUbiquitousStorageAvailablityDidChangeNotification"