Sites no longer load with batch requests & load improvements.
This commit is contained in:
parent
57f275c471
commit
d62c6b4594
@ -183,12 +183,12 @@ PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted );
|
|||||||
} );
|
} );
|
||||||
|
|
||||||
// Do nothing if already fully set up, otherwise (re-)load the store.
|
// Do nothing if already fully set up, otherwise (re-)load the store.
|
||||||
if (self.storeCoordinator && self.mainManagedObjectContext && self.privateManagedObjectContext)
|
if (self.mainManagedObjectContext && self.privateManagedObjectContext)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
[self.storeQueue addOperationWithBlock:^{
|
[self.storeQueue addOperationWithBlock:^{
|
||||||
// Do nothing if already fully set up, otherwise (re-)load the store.
|
// Do nothing if already fully set up, otherwise (re-)load the store.
|
||||||
if (self.storeCoordinator && self.mainManagedObjectContext && self.privateManagedObjectContext)
|
if (self.mainManagedObjectContext && self.privateManagedObjectContext)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Unregister any existing observers and contexts.
|
// Unregister any existing observers and contexts.
|
||||||
@ -201,6 +201,12 @@ PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted );
|
|||||||
[self.privateManagedObjectContext reset];
|
[self.privateManagedObjectContext reset];
|
||||||
self.privateManagedObjectContext = nil;
|
self.privateManagedObjectContext = nil;
|
||||||
}];
|
}];
|
||||||
|
NSError *error = nil;
|
||||||
|
for (NSPersistentStore *store in self.storeCoordinator.persistentStores)
|
||||||
|
if (![self.storeCoordinator removePersistentStore:store error:&error] || error) {
|
||||||
|
MPError( error, @"Couldn't remove persistence store from coordinator." );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Don't load when the store is corrupted.
|
// Don't load when the store is corrupted.
|
||||||
if ([self.storeCorrupted boolValue])
|
if ([self.storeCorrupted boolValue])
|
||||||
@ -209,33 +215,7 @@ PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted );
|
|||||||
// Check if migration is necessary.
|
// Check if migration is necessary.
|
||||||
[self migrateStore];
|
[self migrateStore];
|
||||||
|
|
||||||
// Install managed object contexts and observers.
|
|
||||||
self.privateManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
|
|
||||||
[self.privateManagedObjectContext performBlockAndWait:^{
|
|
||||||
self.privateManagedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
|
|
||||||
self.privateManagedObjectContext.persistentStoreCoordinator = self.storeCoordinator;
|
|
||||||
}];
|
|
||||||
|
|
||||||
self.mainManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
|
|
||||||
self.mainManagedObjectContext.parentContext = self.privateManagedObjectContext;
|
|
||||||
if (@available( iOS 10.0, macOS 10.12, * ))
|
|
||||||
self.mainManagedObjectContext.automaticallyMergesChangesFromParent = YES;
|
|
||||||
else
|
|
||||||
// When privateManagedObjectContext is saved, import the changes into mainManagedObjectContext.
|
|
||||||
PearlAddNotificationObserverTo( self.mainManagedObjectContext, NSManagedObjectContextDidSaveNotification,
|
|
||||||
self.privateManagedObjectContext, nil, ^(NSManagedObjectContext *mainContext, NSNotification *note) {
|
|
||||||
[mainContext performBlock:^{
|
|
||||||
@try {
|
|
||||||
[mainContext mergeChangesFromContextDidSaveNotification:note];
|
|
||||||
}
|
|
||||||
@catch (NSException *exception) {
|
|
||||||
err( @"While merging changes:\n%@", [exception fullDescription] );
|
|
||||||
}
|
|
||||||
}];
|
|
||||||
} );
|
|
||||||
|
|
||||||
// Create a new store coordinator.
|
// Create a new store coordinator.
|
||||||
NSError *error = nil;
|
|
||||||
NSURL *localStoreURL = [self localStoreURL];
|
NSURL *localStoreURL = [self localStoreURL];
|
||||||
if (![[NSFileManager defaultManager] createDirectoryAtURL:[localStoreURL URLByDeletingLastPathComponent]
|
if (![[NSFileManager defaultManager] createDirectoryAtURL:[localStoreURL URLByDeletingLastPathComponent]
|
||||||
withIntermediateDirectories:YES attributes:nil error:&error]) {
|
withIntermediateDirectories:YES attributes:nil error:&error]) {
|
||||||
@ -261,6 +241,29 @@ PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted );
|
|||||||
}
|
}
|
||||||
self.storeCorrupted = @NO;
|
self.storeCorrupted = @NO;
|
||||||
|
|
||||||
|
// Install managed object contexts and observers.
|
||||||
|
self.privateManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
|
||||||
|
self.privateManagedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
|
||||||
|
self.privateManagedObjectContext.persistentStoreCoordinator = self.storeCoordinator;
|
||||||
|
|
||||||
|
self.mainManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
|
||||||
|
self.mainManagedObjectContext.parentContext = self.privateManagedObjectContext;
|
||||||
|
if (@available( iOS 10.0, macOS 10.12, * ))
|
||||||
|
self.mainManagedObjectContext.automaticallyMergesChangesFromParent = YES;
|
||||||
|
else
|
||||||
|
// When privateManagedObjectContext is saved, import the changes into mainManagedObjectContext.
|
||||||
|
PearlAddNotificationObserverTo( self.mainManagedObjectContext, NSManagedObjectContextDidSaveNotification,
|
||||||
|
self.privateManagedObjectContext, nil, ^(NSManagedObjectContext *mainContext, NSNotification *note) {
|
||||||
|
[mainContext performBlock:^{
|
||||||
|
@try {
|
||||||
|
[mainContext mergeChangesFromContextDidSaveNotification:note];
|
||||||
|
}
|
||||||
|
@catch (NSException *exception) {
|
||||||
|
err( @"While merging changes:\n%@", [exception fullDescription] );
|
||||||
|
}
|
||||||
|
}];
|
||||||
|
} );
|
||||||
|
|
||||||
#if TARGET_OS_IPHONE
|
#if TARGET_OS_IPHONE
|
||||||
PearlAddNotificationObserver( UIApplicationWillResignActiveNotification, UIApp, [NSOperationQueue mainQueue],
|
PearlAddNotificationObserver( UIApplicationWillResignActiveNotification, UIApp, [NSOperationQueue mainQueue],
|
||||||
^(MPAppDelegate_Shared *self, NSNotification *note) {
|
^(MPAppDelegate_Shared *self, NSNotification *note) {
|
||||||
|
@ -199,11 +199,7 @@ typedef NS_OPTIONS( NSUInteger, MPPasswordsTips ) {
|
|||||||
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
|
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
|
||||||
|
|
||||||
if (controller == self.fetchedResultsController)
|
if (controller == self.fetchedResultsController)
|
||||||
PearlMainQueue( ^{
|
[self updateSites];
|
||||||
[self.collectionView updateDataSource:self.dataSource
|
|
||||||
toSections:[self createDataSource]
|
|
||||||
reloadItems:nil completion:nil];
|
|
||||||
} );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - UISearchBarDelegate
|
#pragma mark - UISearchBarDelegate
|
||||||
@ -385,9 +381,15 @@ typedef NS_OPTIONS( NSUInteger, MPPasswordsTips ) {
|
|||||||
NSError *error = nil;
|
NSError *error = nil;
|
||||||
self.fetchedResultsController.fetchRequest.predicate =
|
self.fetchedResultsController.fetchRequest.predicate =
|
||||||
[NSPredicate predicateWithFormat:@"name LIKE[cd] %@ AND user == %@", queryPattern, [MPiOSAppDelegate get].activeUserOID];
|
[NSPredicate predicateWithFormat:@"name LIKE[cd] %@ AND user == %@", queryPattern, [MPiOSAppDelegate get].activeUserOID];
|
||||||
if (![self.fetchedResultsController performFetch:&error])
|
if (![self.fetchedResultsController performFetch:&error] || error)
|
||||||
MPError( error, @"Couldn't fetch sites." );
|
MPError( error, @"Couldn't fetch sites." );
|
||||||
|
|
||||||
|
[self updateSites];
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)updateSites {
|
||||||
|
|
||||||
PearlMainQueue( ^{
|
PearlMainQueue( ^{
|
||||||
[self.collectionView updateDataSource:self.dataSource
|
[self.collectionView updateDataSource:self.dataSource
|
||||||
toSections:[self createDataSource]
|
toSections:[self createDataSource]
|
||||||
@ -396,7 +398,6 @@ typedef NS_OPTIONS( NSUInteger, MPPasswordsTips ) {
|
|||||||
[cell setQueryGroups:self.queryGroups];
|
[cell setQueryGroups:self.queryGroups];
|
||||||
}];
|
}];
|
||||||
} );
|
} );
|
||||||
}];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Properties
|
#pragma mark - Properties
|
||||||
@ -416,14 +417,19 @@ typedef NS_OPTIONS( NSUInteger, MPPasswordsTips ) {
|
|||||||
|
|
||||||
if (!_fetchedResultsController) {
|
if (!_fetchedResultsController) {
|
||||||
[MPiOSAppDelegate managedObjectContextForMainThreadPerformBlockAndWait:^(NSManagedObjectContext *mainContext) {
|
[MPiOSAppDelegate managedObjectContextForMainThreadPerformBlockAndWait:^(NSManagedObjectContext *mainContext) {
|
||||||
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass( [MPSiteEntity class] )];
|
NSFetchRequest *fetchRequest = [MPSiteEntity fetchRequest];
|
||||||
fetchRequest.sortDescriptors = @[
|
fetchRequest.sortDescriptors = @[
|
||||||
[[NSSortDescriptor alloc] initWithKey:NSStringFromSelector( @selector( lastUsed ) ) ascending:NO]
|
[[NSSortDescriptor alloc] initWithKey:NSStringFromSelector( @selector( lastUsed ) ) ascending:NO]
|
||||||
];
|
];
|
||||||
fetchRequest.fetchBatchSize = 10;
|
|
||||||
(self.fetchedResultsController = [[NSFetchedResultsController alloc]
|
(self.fetchedResultsController = [[NSFetchedResultsController alloc]
|
||||||
initWithFetchRequest:fetchRequest managedObjectContext:mainContext
|
initWithFetchRequest:fetchRequest managedObjectContext:mainContext
|
||||||
sectionNameKeyPath:nil cacheName:nil]).delegate = self;
|
sectionNameKeyPath:nil cacheName:nil]).delegate = self;
|
||||||
|
|
||||||
|
NSError *error = nil;
|
||||||
|
if (![self.fetchedResultsController performFetch:&error] || error)
|
||||||
|
MPError( error, @"Couldn't fetch sites." );
|
||||||
|
[self updateSites];
|
||||||
}];
|
}];
|
||||||
[self registerObservers];
|
[self registerObservers];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user