iOS/Restkit application design

2019-04-17 12:10发布

问题:

I'm currently developing an application for iOS that is backed by rails. For the communication between iOS and rails i use RESTkit framework since it takes away a lot of work!

I have some doubts on how to manage the code when it starts to grow! How do you design your applications when you use RESTKit? What kind of data layer do you provide to your controllers to perform different actions?

Thanks

回答1:

I usually prefer to create a singleton data controller which provides an API in terms of the model objects and the human understandable operation being performed (getPost, addCommentToPost, createPost, ...). This gives one place that controllers go to get data and means I don't need to pass the data controller around. It also means that all of the mappings are in one place and are isolated from the rest of the code (so when the server changes I don't need to change any code in the controllers, just the code which maps into the model objects).



回答2:

I am not aware of what your goal is with the application that your building in.But to start with i suggest you to create your own custom class(for example: Click this link,which indeed accepts the request(might be POST/GET/PUT) that your making and throws you the details in json format.

On the server side,create the REST api(i prefer php) bridge such that you can able to access the server database.

To start with make login authentication test using POST method(i prefer this because its more secured).

After the login page,i assume you want to show the list of data related to rail,then use UITableView/UICollectionView/Custom GridView.It depends on your requirement.And use the asynchronous approach to send the request but below i haven't used that way ;-)

Example: For login authentication

NSString *post =[[NSString alloc] initWithFormat:@"userName=user&password=pwd"];
NSURL *url=[NSURL URLWithString:@"Your URL/authenticate"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];**
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request   delegate:self];
if( theConnection )
{
    receivedData = [NSMutableData data];
}
else
{
    NSLog(@"theConnection is NULL");
}

NOTE: Always try to return the response in json format.

Q: WHY REST-API WITH JSON? WHY NOT SOAP?

==> Many enterprises are creating mobile applications for their internal staff, for their customers, or both. These applications need access to data, business rules, and business processes. For architectural and security reasons these applications are typically built to access remotes services that provide the data and functionality that are required by the users. That's why All of Yahoo's web services use REST.

-FASTER: REST is almost always going to be faster.

-LOW BANDWIDTH: REST is much more lightweight. For mobile devices even with low bandwidth & network, Restful service works well for mobile devices.

-LOW MEMORY CONSUMPTION: The important/must thing in the mobile devices is how we handle the memory when running our application. REST always uses less memory without any unwanted xml strings.

Concerning REST or SOAP, the last one is indeed really heavy for mobile platform and not so easy to implement. SOAP requires XML too and cannot be used with JSON. Whereas with REST you can use JSON or XML and easily implement it on mobiles with RESTKit (http://restkit.org/), for security we can use an SSL connection with HTTPS and a signed certificate.

Source: http://en.wikipedia.org/wiki/Representational_state_transfer

I believe that the information what i have given above is still not enough,you need to do a google a bit.(http://www.restapitutorial.com)