Skip to content

Instantly share code, notes, and snippets.

@mteece
Created June 8, 2016 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mteece/f47e3933726786c33a1d3db8790ad2cf to your computer and use it in GitHub Desktop.
Save mteece/f47e3933726786c33a1d3db8790ad2cf to your computer and use it in GitHub Desktop.
Passing RLMResults into a RLMArray for use in swapping objects. To avoid using sort index properties and rely on the array indices.
#import "AppDelegate.h"
#import <Realm/Realm.h>
// Define your models
@interface Dog : RLMObject
@property NSString *name;
@property NSInteger age;
@end
@implementation Dog
// No need for implementation
@end
RLM_ARRAY_TYPE(Dog)
@interface Person : RLMObject
@property NSString *name;
@property RLMArray<Dog> *dogs;
@end
@implementation Person
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UIViewController alloc] init];
[self.window makeKeyAndVisible];
[[NSFileManager defaultManager] removeItemAtPath:[RLMRealmConfiguration defaultConfiguration].path error:nil];
NSArray *names = @[@"Rex", @"Jim", @"Max", @"Stella"];
NSArray *ages = @[@9, @14, @4, @12];
NSMutableArray *kennel = [[NSMutableArray alloc] init];
for (int i = 0; i <= 3; i++) {
// Create a standalone object
Dog *mydog = [[Dog alloc] init];
NSString *name = [names objectAtIndex:i];
NSInteger age = [[ages objectAtIndex: i ] integerValue];
// Set & read properties
mydog.name = name;
mydog.age = age;
[kennel addObject:mydog];
}
NSLog(@"Current Kennel Description: %@", kennel);
// Realms are used to group data together
RLMRealm *realm = [RLMRealm defaultRealm]; // Create realm pointing to default file
// Save your dogd and kennel
[realm beginWriteTransaction];
[realm addObjects:kennel];
[realm commitWriteTransaction];
// RLMResults are auto-updating container type, chages are reflected on disk in a transaction.
RLMResults *results = [Dog allObjectsInRealm:realm];
NSLog(@"Current Kennel Stored: %@", results);
RLMArray * swappableArray = [[RLMArray alloc] initWithObjectClassName:[Dog className]];
for (Dog *object in results) {
// Create a standalone object
Dog *dogStandAlone = [[Dog alloc] init];
dogStandAlone.name = object.name;
dogStandAlone.age = object.age;
// Accessors cannot be mutated or stored.
[swappableArray addObject:dogStandAlone];
}
NSLog(@"Swappable Kennel Description: %@", swappableArray);
// Delete your initial objects
[realm beginWriteTransaction];
[realm deleteObjects:kennel];
[realm commitWriteTransaction];
// Swap stuff
id tempObject = [swappableArray objectAtIndex:1];
[swappableArray replaceObjectAtIndex:1 withObject:[swappableArray objectAtIndex:3]];
[swappableArray replaceObjectAtIndex:3 withObject:tempObject];
// Save your objects, they were copied over
[realm beginWriteTransaction];
[realm addObjects:swappableArray];
[realm commitWriteTransaction];
RLMResults *swappedResults = [Dog allObjectsInRealm:realm];
NSLog(@"Kennel Swapped Description: %@", swappedResults);
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment