mirror of
https://github.com/moonlight-stream/moonlight-qt.git
synced 2026-06-17 14:11:33 +00:00
Use a temporary app object and database lock to avoid saving while the database is not in a consistent state
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#import "AppDelegate.h"
|
||||
#import "Host.h"
|
||||
#import "App.h"
|
||||
#import "TemporaryApp.h"
|
||||
|
||||
@interface DataManager : NSObject
|
||||
|
||||
@@ -22,7 +23,7 @@
|
||||
- (void) saveData;
|
||||
- (Host*) createHost;
|
||||
- (void) removeHost:(Host*)host;
|
||||
- (App*) createApp;
|
||||
- (void) removeApp:(App*)app;
|
||||
- (App*) addAppFromTemporaryApp:(TemporaryApp*)tempApp;
|
||||
- (void) removeAppFromHost:(App*)app;
|
||||
|
||||
@end
|
||||
|
||||
@@ -7,9 +7,18 @@
|
||||
//
|
||||
|
||||
#import "DataManager.h"
|
||||
#import "TemporaryApp.h"
|
||||
|
||||
@implementation DataManager
|
||||
|
||||
+ (NSObject *)databaseLock {
|
||||
static NSObject *lock = nil;
|
||||
if (lock == nil) {
|
||||
lock = [[NSObject alloc] init];
|
||||
}
|
||||
return lock;
|
||||
}
|
||||
|
||||
- (id) init {
|
||||
self = [super init];
|
||||
self.appDelegate = [[UIApplication sharedApplication] delegate];
|
||||
@@ -57,38 +66,65 @@
|
||||
}
|
||||
|
||||
- (void) saveData {
|
||||
NSError* error;
|
||||
if (![[self.appDelegate managedObjectContext] save:&error]) {
|
||||
Log(LOG_E, @"Unable to save hosts to database: %@", error);
|
||||
@synchronized([DataManager databaseLock]) {
|
||||
NSError* error;
|
||||
if (![[self.appDelegate managedObjectContext] save:&error]) {
|
||||
Log(LOG_E, @"Unable to save hosts to database: %@", error);
|
||||
}
|
||||
[self.appDelegate saveContext];
|
||||
}
|
||||
[self.appDelegate saveContext];
|
||||
}
|
||||
|
||||
- (NSArray*) retrieveHosts {
|
||||
return [self fetchRecords:@"Host"];
|
||||
}
|
||||
|
||||
- (App*) createApp {
|
||||
NSEntityDescription* entity = [NSEntityDescription entityForName:@"App" inManagedObjectContext:[self.appDelegate managedObjectContext]];
|
||||
return [[App alloc] initWithEntity:entity insertIntoManagedObjectContext:[self.appDelegate managedObjectContext]];
|
||||
- (App*) addAppFromTemporaryApp:(TemporaryApp*)tempApp {
|
||||
|
||||
App* managedApp;
|
||||
|
||||
@synchronized([DataManager databaseLock]) {
|
||||
NSEntityDescription* entity = [NSEntityDescription entityForName:@"App" inManagedObjectContext:[self.appDelegate managedObjectContext]];
|
||||
managedApp = [[App alloc] initWithEntity:entity insertIntoManagedObjectContext:[self.appDelegate managedObjectContext]];
|
||||
|
||||
assert(tempApp.host != nil);
|
||||
|
||||
managedApp.id = tempApp.id;
|
||||
managedApp.image = tempApp.image;
|
||||
managedApp.name = tempApp.name;
|
||||
managedApp.isRunning = tempApp.isRunning;
|
||||
managedApp.host = tempApp.host;
|
||||
|
||||
[managedApp.host addAppListObject:managedApp];
|
||||
}
|
||||
|
||||
return managedApp;
|
||||
}
|
||||
|
||||
- (void) removeApp:(App*)app {
|
||||
[[self.appDelegate managedObjectContext] deleteObject:app];
|
||||
- (void) removeAppFromHost:(App*)app {
|
||||
@synchronized([DataManager databaseLock]) {
|
||||
assert(app.host != nil);
|
||||
|
||||
[app.host removeAppListObject:app];
|
||||
[[self.appDelegate managedObjectContext] deleteObject:app];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSArray*) fetchRecords:(NSString*)entityName {
|
||||
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
|
||||
NSEntityDescription* entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self.appDelegate managedObjectContext]];
|
||||
[fetchRequest setEntity:entity];
|
||||
[fetchRequest setAffectedStores:[NSArray arrayWithObjects:[[self.appDelegate persistentStoreCoordinator] persistentStoreForURL:[self.appDelegate getStoreURL]], nil]];
|
||||
NSArray* fetchedRecords;
|
||||
|
||||
NSError* error;
|
||||
NSArray* fetchedRecords = [[self.appDelegate managedObjectContext] executeFetchRequest:fetchRequest error:&error];
|
||||
//TODO: handle errors
|
||||
@synchronized([DataManager databaseLock]) {
|
||||
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
|
||||
NSEntityDescription* entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self.appDelegate managedObjectContext]];
|
||||
[fetchRequest setEntity:entity];
|
||||
[fetchRequest setAffectedStores:[NSArray arrayWithObjects:[[self.appDelegate persistentStoreCoordinator] persistentStoreForURL:[self.appDelegate getStoreURL]], nil]];
|
||||
|
||||
NSError* error;
|
||||
fetchedRecords = [[self.appDelegate managedObjectContext] executeFetchRequest:fetchRequest error:&error];
|
||||
//TODO: handle errors
|
||||
}
|
||||
|
||||
return fetchedRecords;
|
||||
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// TemporaryApp.h
|
||||
// Moonlight
|
||||
//
|
||||
// Created by Cameron Gutman on 9/30/15.
|
||||
// Copyright © 2015 Moonlight Stream. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "Host.h"
|
||||
|
||||
@interface TemporaryApp : NSObject
|
||||
|
||||
@property (nullable, nonatomic, retain) NSString *id;
|
||||
@property (nullable, nonatomic, retain) NSData *image;
|
||||
@property (nullable, nonatomic, retain) NSString *name;
|
||||
@property (nonatomic) BOOL isRunning;
|
||||
@property (nullable, nonatomic, retain) Host *host;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// TemporaryApp.m
|
||||
// Moonlight
|
||||
//
|
||||
// Created by Cameron Gutman on 9/30/15.
|
||||
// Copyright © 2015 Moonlight Stream. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TemporaryApp.h"
|
||||
|
||||
@implementation TemporaryApp
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user