inaka

Latest blog entries

/
The Art of Writing a Blogpost

The Art of Writing a Blogpost

Apr 11 2017 : Matias Vera

/
SpellingCI: No more spelling mistakes in your markdown flies!

Feb 14 2017 : Felipe Ripoll

/
Fast reverse geocoding with offline-geocoder

Do you need a blazing fast reverse geocoder? Enter offline-geocoder!

Jan 18 2017 : Roberto Romero

/
Using Jayme to connect to the new MongooseIM REST services

MongooseIM has RESTful services!! Here I show how you can use them in an iOS application.

Dec 13 2016 : Sergio Abraham

/
20 Questions, or Maybe a Few More

20 Questions, or Maybe a Few More

Nov 16 2016 : Stephanie Goldner

/
The Power of Meeting People

Because conferences and meetups are not just about the technical stuff.

Nov 01 2016 : Pablo Villar

/
Finding the right partner for your app build

Sharing some light on how it is to partner with us.

Oct 27 2016 : Inaka

/
Just Play my Sound

How to easily play a sound in Android

Oct 25 2016 : Giaquinta Emiliano

/
Opening our Guidelines to the World

We're publishing our work guidelines for the world to see.

Oct 13 2016 : Brujo Benavides

/
Using NIFs: the easy way

Using niffy to simplify working with NIFs on Erlang

Oct 05 2016 : Hernan Rivas Acosta

/
Function Naming In Swift 3

How to write clear function signatures, yet expressive, while following Swift 3 API design guidelines.

Sep 16 2016 : Pablo Villar

/
Jenkins automated tests for Rails

How to automatically trigger rails tests with a Jenkins job

Sep 14 2016 : Demian Sciessere

/
Erlang REST Server Stack

A description of our usual stack for building REST servers in Erlang

Sep 06 2016 : Brujo Benavides

/
Replacing JSON when talking to Erlang

Using Erlang's External Term Format

Aug 17 2016 : Hernan Rivas Acosta

/
Gadget + Lewis = Android Lint CI

Integrating our Android linter with Github's pull requests

Aug 04 2016 : Fernando Ramirez and Euen Lopez

/
Passwordless login with phoenix

Introducing how to implement passwordless login with phoenix framework

Jul 27 2016 : Thiago Borges

/
Beam Olympics

Our newest game to test your Beam Skills

Jul 14 2016 : Brujo Benavides

/
Otec

Three Open Source Projects, one App

Jun 28 2016 : Andrés Gerace

/
CredoCI

Running credo checks for elixir code on your github pull requests

Jun 16 2016 : Alejandro Mataloni

/
Thoughts on rebar3

Thoughts on rebar3

Jun 08 2016 : Hernán Rivas Acosta

/
See all Inaka's blog posts >>

/
Core Data One-Way Relationships

Tom Ryan wrote this on March 06, 2013 under coredata, dev, ios, mobile-development .

Core Data Makes Things Simple

I really love using Core Data. It's made writing a data store-backed app pretty dead easy.

There are times when it can cumbersome, and I'd like to take a look at one such example.

Core Data relationships need to be two-way. This can be annoying when you want to store an object as a property, and seemingly the only way to do that is to create a model for that object and define a relationship for it.

But you don't always want a back-relationship.

Modeling One-Way Relationships

Let's say I want to model a User.

This User will have a first name, last name, and a bunch of attributes that may differ from user to user -- for example, an NSDictionary with possibly different sets of keys.

First and last names are easy enough to handle -- just make their property type NSStrings. The heterogeneous attributes are a different matter. Core Data doesn't allow objects like dictionaries as raw properties, so we have to do some magic with NSValueTransformer.

So, once the NSManagedObject subclass has been generated, it's often easiest to set the property type to the actual type you'll be storing. In this case, it's an NSDictionary.

@interface User : NSManagedObject

    @property (nonatomic, retain) NSString * firstName;
    @property (nonatomic, retain) NSString * lastName;
    //@property (nonatomic, retain) id payload;
    /* Changing the property type to NSDictionary so I don't have to worry 
       about casting it when I call it */
    @property (nonatomic, retain) NSDictionary *payload;

    @end

Note: I generally like to make any changes to the NSManagedObject class in a category, so when I have to make changes to the model, I won't have to worry about overwriting any changes I've made.

Now it's time to create the PayloadTransformer class, which is a subclass of NSValueTransformer:

#import "PayloadTransformer.h"

    @implementation PayloadTransformer
    + (BOOL)allowsReverseTransformation {
        return YES;
    }
    
    + (Class)transformedValueClass {
        return [NSData class];
    }
    
    - (id)transformedValue:(id)value {
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:value];
        return data;
    }
    
    - (id)reverseTransformedValue:(id)value {
        NSDictionary *payload = [NSKeyedUnarchiver unarchiveObjectWithData:value];
        return payload;
    }
    @end

We're Done!

Excellent, we're done! So, how is this actually used?

- (void)addUser {
      User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:self.managedObjectContext];
      user.firstName = @"Wil";
      user.lastName = @"Wheaton";
      NSDictionary *userPayload = @{@"Favorite Color":@"Blue", @"Best Buddy":@"Floxotron"};
      user.payload = userPayload;
      NSError *saveError = nil;
      if(![self.managedObjectContext save:&saveError]) {
        NSLog(@"Hey, error! %@", saveError);
      }
    }

    - (void)getWil {
      NSFetchRequest *wilRequest = [[NSFetchRequest alloc] init];
      wilRequest.entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:self.managedObjectContext];
      wilRequest.predicate = [NSPredicate predicateWithFormat:@"firstName=%@ AND lastName=%@", @"Wil", @"Wheaton"];
      NSError *wilError = nil;
      User *wilWheaton = [[self.managedObjectContext executeFetchRequest:wilRequest error:&wilError] lastObject];
      if(wilWheaton) {
        NSDictionary *payload = wilWheaton.payload;
        NSLog(@"Payload: %@", payload);
      }
    }

This is the simplest way I've found to simulate a one-way relationship in Core Data. Enjoy!