forked from magicalpanda/MagicalRecord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARCoreDataAction.m
94 lines (73 loc) · 2.72 KB
/
ARCoreDataAction.m
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
//
// ARCoreDataAction.m
// Freshpod
//
// Created by Saul Mora on 2/24/11.
// Copyright 2011 Magical Panda Software. All rights reserved.
//
#import "ARCoreDataAction.h"
static dispatch_queue_t coredata_background_save_queue;
dispatch_queue_t background_save_queue()
{
if (coredata_background_save_queue == NULL)
{
coredata_background_save_queue = dispatch_queue_create("com.magicalpanda.coredata.backgroundsaves", 0);
}
return coredata_background_save_queue;
}
@implementation ARCoreDataAction
#ifdef NS_BLOCKS_AVAILABLE
+ (void) saveDataWithBlock:(void(^)(NSManagedObjectContext *localContext))block
{
NSManagedObjectContext *mainContext = [NSManagedObjectContext defaultContext];
NSManagedObjectContext *localContext = mainContext;
if (![NSThread isMainThread])
{
#if kCreateNewCoordinatorOnBackgroundOperations == 1
NSPersistentStoreCoordinator *localCoordinator = [NSPersistentStoreCoordinator coordinatorWithPersitentStore:[NSPersistentStore defaultPersistentStore]];
localContext = [NSManagedObjectContext contextThatNotifiesDefaultContextOnMainThreadWithCoordinator:localCoordinator];
#else
localContext = [NSManagedObjectContext contextThatNotifiesDefaultContextOnMainThread];
#endif
[mainContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
[localContext setMergePolicy:NSOverwriteMergePolicy];
}
block(localContext);
if ([localContext hasChanges])
{
[localContext save];
}
localContext.notifiesMainContextOnSave = NO;
[mainContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
}
+ (void) saveDataInBackgroundWithBlock:(void(^)(NSManagedObjectContext *localContext))block
{
dispatch_async(background_save_queue(), ^{
[self saveDataWithBlock:block];
});
}
+ (void) saveDataInBackgroundWithBlock:(void(^)(NSManagedObjectContext *localContext))block completion:(void(^)(void))callback
{
dispatch_async(background_save_queue(), ^{
[self saveDataWithBlock:block];
if (callback)
{
dispatch_async(dispatch_get_main_queue(), callback);
}
});
}
+ (void) lookupWithBlock:(void(^)(NSManagedObjectContext *localContext))block
{
NSManagedObjectContext *context = [NSManagedObjectContext contextForCurrentThread];
block(context);
}
+ (void) saveDataWithOptions:(ARCoreDataSaveOption)options withBlock:(void(^)(NSManagedObjectContext *localContext))block;
{
//TODO: add implementation
}
+ (void) saveDataWithOptions:(ARCoreDataSaveOption)options withBlock:(void(^)(NSManagedObjectContext *localContext))block completion:(void(^)(void))callback;
{
//TODO: add implementation
}
#endif
@end