开发者

IPad App pull and push relational data

Im currently putting together a business app for an ipad. It will be talking to a Microsoft SQL server database.

My question is what is the most efficient way to pull relational data over the line. One example is im showing a contact listing in the app. The contact record has a departmentID field (which is related to the department table) and a ContactTypeID field (which is related to the ContactType table). I am hoping that when the user first starts the app I will pull the department and contacttype table data over onto the ipad. Than when i pull the contact listing data I will just pull the ID's for the fields over and pull their related 开发者_高级运维data out of the data i pulled at startup. The user must than be able to click on a record in the listing and bring up the details page for the selected contact. This is a simple example but hopefully it makes my point.

Has anyone got any advice on the best approach for this? I will be needing to both pull data and push data to and from the server.

Thanks in advance


A popular approach is to convert your server-side objects to JSON, and then send the JSON string to the device. On the device, decode JSON into NSDictionary/NSArray values using some JSON framework (I suggest JSONKit since it's very simple and very fast).

Once you have your decoded JSON, you can use (shameless plug warning) this technique to turn your NS* objects into CoreData objects, and save them onto your phone.

As for maintaining relationships, you can either use a nested representation or a flat representation. An example nested implementation would be:

{
    class: "Contact",
    first_name: "John",
    last_name: "Doe",
    contact_type: {
        class: "ContactType",
        type: "some value"
    },
    department: {
        class: "Department",
        name: "Department of Transportation"
    }
}

This is a preferred approach if you have a simple database, with no relationship cycles.

Alternatively, you can use a flat representation:

{
    class: "Contact",
    id: 1,
    first_name: "John",
    last_name: "Doe",
    contact_type_id: 15,
    department_id: 34
}

{
    class: "ContactType",
    id: 15,
    type: "some value"
}

{
    class: "Department",
    id: 34,
    name: "Department of Transportation"
}

Then you would have to resolve the relationships manually using contact_type_id and department_id, on the device.

It's best to test both of these approaches and see which one works better in your particular case. Personally, I'd recommend the nested approach (if your DB layout allows it), since it's much faster and relationship resolution is done on the server (where you can probably afford it), instead of on the device (where you probably can't afford it if you have a large database).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜