Search code examples
iosobjective-cdbaccess

Relating two objects in DBAccess


I'm using dbaccess for my iOS project. How can I pass an array to dbaccess object? For example: I have dbobject like:

@interface Member : DBObject
@property (strong) NSString* firstname;
@property (strong) NSString* lastName;
@end

@interface Group : DBObject
@property (strong) NSString* groupName;
@property (strong) NSString* adminName;
@property (strong) Member* members;
@end

For this group, it has 4 member than How can I store all group members and group detail in one object and also how to retrieve them? Thanx in adv.


Solution

  • To answer the question I have re-modelled and provided an example below of how you would go about creating a one-to-one relationship between these two objects.

    This problem stems from the fact that there is no such thing as a typed array in Objective-c. When there is, we will look at re-implementing how the interface works.

    I have moved the Group object into the member, as a member belongs to a group, and then added a members method to the Group object to look backwards to the child objects.

    @interface Member : DBObject
    
    @property (strong) NSString* firstname;
    @property (strong) NSString* lastName;
    @property (strong) Group* group;
    
    @end
    
    @interface Group : DBObject
    
    @property (strong) NSString* groupName;
    @property (strong) NSString* adminName;
    
    - (DBResultSet*)members;
    
    @end
    

    And the implementations now look like this.

    @implementation Member
    
    @dynamic firstname,lastName, group;
    
    @end
    
    @implementation Group
    
    @dynamic adminName, groupName;
    
    - (DBResultSet *)members {
        /* we achieve one-to-many relationships by querying the objects in the 
         manner a relational database would commonly be structured */
    
        return [[[Member query] whereWithFormat:@"group = %@", self] fetch];
    
    }
    
    - (void)entityDidDelete {
    
        /* when you create tight bonds, you may well wish to create 'trigger' methods 
         to clean data and keep the integrity in good order */
    
        for (Member* member in [[[Member query] whereWithFormat:@"group = %@", self] fetch]) {
            member.group = nil;
            [member commit];
        }
    
    }
    
    @end
    

    Now there is nothing stopping you creating an array as a property type, and storing the Id values within it. But that is not as clean, and requires you to maintain it, whereas if you a looking for FK values, this requires no maintenance and you can create lovely logic to stop the deletion of objects if it is related to others, without having to hydrate lots of objects and then look inside arrays.

    Plus you get the beautiful option of using the object dot notation to navigate the strongly typed relationships from the Person object.

    NSString* admin = person.group.adminName;
    

    Also, when you added the Group object into Member:

    @property (strong) Group* group;
    

    DBAccess automatically created an index in SQLite for the group property, and prioritises its importance within the cache, as objects which are linked this way are more likely to be accessed.

    Hope this helps, Adrian