IPad App pull and push relational data

给你一囗甜甜゛ 提交于 2019-12-12 03:40:35

问题


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


回答1:


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).



来源:https://stackoverflow.com/questions/4182664/ipad-app-pull-and-push-relational-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!