Quellcode durchsuchen

Made Realm integration a dynamic runtime check.

Tim Oliver vor 10 Jahren
Ursprung
Commit
548fd03bd5

+ 23 - 13
Classes/GlobalStateExplorers/DatabaseBrowser/FLEXRealmDatabaseManager.m

@@ -9,25 +9,30 @@
 #import "FLEXRealmDatabaseManager.h"
 #import "FLEXRealmDatabaseManager.h"
 
 
 #if __has_include(<Realm/Realm.h>)
 #if __has_include(<Realm/Realm.h>)
-
 #import <Realm/Realm.h>
 #import <Realm/Realm.h>
 #import <Realm/RLMRealm_Dynamic.h>
 #import <Realm/RLMRealm_Dynamic.h>
+#else
+#import "FLEXRealmDefines.h"
+#endif
 
 
 @interface FLEXRealmDatabaseManager ()
 @interface FLEXRealmDatabaseManager ()
 
 
 @property (nonatomic, copy) NSString *path;
 @property (nonatomic, copy) NSString *path;
-@property (nonatomic, strong) RLMRealm *realm;
+@property (nonatomic, strong) id realm;
 
 
 @end
 @end
 
 
-#endif
+//#endif
 
 
 @implementation FLEXRealmDatabaseManager
 @implementation FLEXRealmDatabaseManager
 
 
-#if __has_include(<Realm/Realm.h>)
-
 - (instancetype)initWithPath:(NSString*)aPath
 - (instancetype)initWithPath:(NSString*)aPath
 {
 {
+    Class realmClass = NSClassFromString(@"RLMRealm");
+    if (realmClass == nil) {
+        return nil;
+    }
+    
     self = [super init];
     self = [super init];
     
     
     if (self) {
     if (self) {
@@ -38,17 +43,24 @@
 
 
 - (BOOL)open
 - (BOOL)open
 {
 {
+    Class realmClass = NSClassFromString(@"RLMRealm");
+    Class configurationClass = NSClassFromString(@"RLMRealmConfiguration");
+    
+    if (realmClass == nil || configurationClass == nil) {
+        return NO;
+    }
+    
     NSError *error = nil;
     NSError *error = nil;
-    RLMRealmConfiguration *configuration = [[RLMRealmConfiguration alloc] init];
-    configuration.path = self.path;
-    self.realm = [RLMRealm realmWithConfiguration:configuration error:&error];
+    id configuration = [[configurationClass alloc] init];
+    [configuration setPath:self.path];
+    self.realm = [realmClass realmWithConfiguration:configuration error:&error];
     return (error == nil);
     return (error == nil);
 }
 }
 
 
 - (NSArray *)queryAllTables
 - (NSArray *)queryAllTables
 {
 {
     NSMutableArray *allTables = [NSMutableArray array];
     NSMutableArray *allTables = [NSMutableArray array];
-    RLMSchema *schema = self.realm.schema;
+    RLMSchema *schema = [self.realm schema];
     
     
     for (RLMObjectSchema *objectSchema in schema.objectSchema) {
     for (RLMObjectSchema *objectSchema in schema.objectSchema) {
         if (objectSchema.className == nil) {
         if (objectSchema.className == nil) {
@@ -64,7 +76,7 @@
 
 
 - (NSArray *)queryAllColumnsWithTableName:(NSString *)tableName
 - (NSArray *)queryAllColumnsWithTableName:(NSString *)tableName
 {
 {
-    RLMObjectSchema *objectSchema = [self.realm.schema schemaForClassName:tableName];
+    RLMObjectSchema *objectSchema = [[self.realm schema] schemaForClassName:tableName];
     if (objectSchema == nil) {
     if (objectSchema == nil) {
         return nil;
         return nil;
     }
     }
@@ -79,7 +91,7 @@
 
 
 - (NSArray *)queryAllDataWithTableName:(NSString *)tableName
 - (NSArray *)queryAllDataWithTableName:(NSString *)tableName
 {
 {
-    RLMObjectSchema *objectSchema = [self.realm.schema schemaForClassName:tableName];
+    RLMObjectSchema *objectSchema = [[self.realm schema] schemaForClassName:tableName];
     RLMResults *results = [self.realm allObjects:tableName];
     RLMResults *results = [self.realm allObjects:tableName];
     if (results.count == 0 || objectSchema == nil) {
     if (results.count == 0 || objectSchema == nil) {
         return nil;
         return nil;
@@ -98,6 +110,4 @@
     return allDataEntries;
     return allDataEntries;
 }
 }
 
 
-#endif
-
 @end
 @end

+ 46 - 0
Classes/GlobalStateExplorers/DatabaseBrowser/FLEXRealmDefines.h

@@ -0,0 +1,46 @@
+//
+//  Realm.h
+//  FLEX
+//
+//  Created by Tim Oliver on 16/02/2016.
+//  Copyright © 2016 Realm. All rights reserved.
+//
+
+#if __has_include(<Realm/Realm.h>)
+#else
+
+@class RLMObject, RLMResults, RLMRealm, RLMRealmConfiguration, RLMSchema, RLMObjectSchema, RLMProperty;
+
+@interface RLMRealmConfiguration : NSObject
+@property (nonatomic, copy) NSString *path;
+@end
+
+@interface RLMRealm : NSObject
+@property (nonatomic, readonly) RLMSchema *schema;
++ (RLMRealm *)realmWithConfiguration:(RLMRealmConfiguration *)configuration error:(NSError **)error;
+- (RLMResults *)allObjects:(NSString *)className;
+@end
+
+@interface RLMSchema : NSObject
+@property (nonatomic, readonly) NSArray *objectSchema;
+- (RLMObjectSchema *)schemaForClassName:(NSString *)className;
+@end
+
+@interface RLMObjectSchema : NSObject
+@property (nonatomic, readonly) NSString *className;
+@property (nonatomic, readonly) NSArray *properties;
+@end
+
+@interface RLMProperty : NSString
+@property (nonatomic, readonly) NSString *name;
+@end
+
+@interface RLMResults : NSObject <NSFastEnumeration>
+@property (nonatomic, readonly) NSInteger count;
+@end
+
+@interface RLMObject : NSObject
+
+@end
+
+#endif

+ 1 - 6
Classes/GlobalStateExplorers/DatabaseBrowser/FLEXTableListViewController.m

@@ -10,9 +10,7 @@
 
 
 #import "FLEXDatabaseManager.h"
 #import "FLEXDatabaseManager.h"
 #import "FLEXSQLiteDatabaseManager.h"
 #import "FLEXSQLiteDatabaseManager.h"
-#if __has_include(<Realm/Realm.h>)
 #import "FLEXRealmDatabaseManager.h"
 #import "FLEXRealmDatabaseManager.h"
-#endif
 
 
 #import "FLEXTableContentViewController.h"
 #import "FLEXTableContentViewController.h"
 
 
@@ -48,12 +46,9 @@
     if ([@[@"db", @"sqlite", @"sqlite3"] indexOfObject:pathExtension] != NSNotFound) {
     if ([@[@"db", @"sqlite", @"sqlite3"] indexOfObject:pathExtension] != NSNotFound) {
         return [[FLEXSQLiteDatabaseManager alloc] initWithPath:path];
         return [[FLEXSQLiteDatabaseManager alloc] initWithPath:path];
     }
     }
-    
-#if __has_include(<Realm/Realm.h>)
-    if ([pathExtension isEqualToString:@"realm"]) {
+    else if ([pathExtension isEqualToString:@"realm"]) {
         return [[FLEXRealmDatabaseManager alloc] initWithPath:path];
         return [[FLEXRealmDatabaseManager alloc] initWithPath:path];
     }
     }
-#endif
     
     
     return nil;
     return nil;
 }
 }

+ 2 - 4
Classes/GlobalStateExplorers/FLEXFileBrowserTableViewController.m

@@ -218,13 +218,11 @@
             } else if ([FLEXWebViewController supportsPathExtension:pathExtension]) {
             } else if ([FLEXWebViewController supportsPathExtension:pathExtension]) {
                 drillInViewController = [[FLEXWebViewController alloc] initWithURL:[NSURL fileURLWithPath:fullPath]];
                 drillInViewController = [[FLEXWebViewController alloc] initWithURL:[NSURL fileURLWithPath:fullPath]];
             } else if ([@[@"db", @"sqlite", @"sqlite3"] containsObject:[subpath pathExtension]]) {
             } else if ([@[@"db", @"sqlite", @"sqlite3"] containsObject:[subpath pathExtension]]) {
-              drillInViewController = [[FLEXTableListViewController alloc] initWithPath:fullPath];
+                drillInViewController = [[FLEXTableListViewController alloc] initWithPath:fullPath];
             }
             }
-#if __has_include(<Realm/Realm.h>)
-            else if ([@[@"realm"] containsObject:[subpath pathExtension]]) {
+            else if (NSClassFromString(@"RLMRealm") && [@[@"realm"] containsObject:[subpath pathExtension]]) {
                 drillInViewController = [[FLEXTableListViewController alloc] initWithPath:fullPath];
                 drillInViewController = [[FLEXTableListViewController alloc] initWithPath:fullPath];
             }
             }
-#endif
             else {
             else {
                 NSString *fileString = [NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:NULL];
                 NSString *fileString = [NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:NULL];
                 if ([fileString length] > 0) {
                 if ([fileString length] > 0) {