forked from RestKit/RestKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Upgrading from v0.10.x to v0.20.0
drewish edited this page Nov 12, 2012
·
33 revisions
One of the big changes is that the custom networking code has been dropped and replaced by the widely used AFNetworking library.
in 0.10:
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURLString:url];
[objectManager.client setValue:@"foobar" forHTTPHeaderField:@"X-Custom-Header"];
in 0.20:
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURLString:url];
[objectManager.HTTPClient setDefaultHeader:@"X-Custom-Header" value:@"foobar"];
in 0.10:
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager.client.requestCache invalidateAll];
in 0.20:
[[ NSURLCache sharedURLCache] removeAllCachedResponses]
in 0.10:
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURLString:url];
objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
in 0.20:
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
The sample code assumes you've registered a POST route for your object in the object manager's route set.
in 0.10:
RKObjectManager *mgr = [RKObjectManager sharedManager];
[mgr postObject:obj usingBlock:^(RKObjectLoader *loader) {
RKParams* params = [RKParams params];
[params setValue:obj.name forParam:@"expense[name]"];
[params setValue:obj.amount forParam:@"expense[amount]"];
RKParamsAttachment *attachment = [params setData:UIImageJPEGRepresentation(obj.photo, 0.7)
MIMEType:@"image/jpeg"
forParam:@"expense[photo_file]"];
attachment.fileName = @"image.jpg";
loader.params = params;
// What ever success handler you need.
loader.onDidLoadObject = ^(id loaded) {
NSLog(@"%@", loaded);
};
}];
in 0.20:
// But this time assuming you registered a RKRequestDescriptor to handle
// serializing the other fields.
RKObjectManager *objectManager = [RKObjectManager sharedManager];
NSMutableURLRequest *request =
[objectManager multipartFormRequestWithObject:obj method:RKRequestMethodPOST
path:nil parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formData appendPartWithFileData:UIImageJPEGRepresentation(obj.photo, 0.7)
name:@"expense[photo_file]"
fileName:@"photo.jpg"
mimeType:@"image/jpeg"];
}];
RKObjectRequestOperation *operation =
[objectManager objectRequestOperationWithRequest:request
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
// Success handler.
NSLog(@"%@", [mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
// Error handler.
}];
The big change is the lack of a loader object and the new more consistent callback signature with RKMappingResult wraps the results of the mapping.
In 0.10:
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/examples" usingBlock:^(RKObjectLoader *loader) {
loader.onDidLoadObjects = ^(NSArray *objects) {
NSLog(@"It Worked: %@", objects);
};
loader.onDidFailWithError = ^(NSError *error) {
NSLog(@"It Failed: %@", error);
};
}];
In 0.20:
[RKObjectManager.sharedManager getObjectsAtPath:path parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
NSLog(@"It Worked: %@", [mappingResult array]);
// Or if you're only expecting a single object:
NSLog(@"It Worked: %@", [mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"It Failed: %@", error);
}];
Serialization has changed quite since the request and response have have have separate mappings.
In 0.10:
RKObjectMapping *serialization = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[serialization mapKeyPath:@"amount" toAttribute:@"reimbursal[amount]"];
[serialization mapKeyPath:@"fromUserId" toAttribute:@"reimbursal[from_user_id]"];
[serialization mapKeyPath:@"toUserId" toAttribute:@"reimbursal[to_user_id]"];
[provider setSerializationMapping:serialization forClass:[RMReimbursal class]];
In 0.20:
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{
@"amount": @"amount",
@"fromUserId": @"from_user_id",
@"toUserId": @"to_user_id",
}];
[objectManager addRequestDescriptor:
[RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[RMReimbursal class]
rootKeyPath:@"reimbursal"]];
TODO: summarize the changes.
In 0.10:
// Defined elsewhere:
RKMapping *userMapping;
RKManagedObjectStore *objectStore
// The meat of it:
RKEntityMapping* mapping = [RKEntityMapping
mappingForEntityForName:@"RMHousehold"
inManagedObjectStore:objectStore];
mapping.primaryKeyAttribute = @"householdId";
[mapping mapKeyPath:@"id" toAttribute:@"householdId"];
[mapping mapKeyPath:@"display_name" toAttribute:@"displayName"];
[mapping mapKeyPath:@"users" toRelationship:@"users"
withMapping:userMapping];
In 0.20:
// Defined elsewhere:
RKMapping *userMapping;
RKManagedObjectStore *objectStore;
RKEntityMapping* mapping = [RKEntityMapping
mappingForEntityForName:@"RMHousehold"
inManagedObjectStore:objectStore];
mapping.primaryKeyAttribute = @"householdId";
[mapping addAttributeMappingsFromDictionary:@{
@"id": @"householdId",
@"display_name": @"displayName",
}];
RKRelationshipMapping *userRelationship =
[RKRelationshipMapping relationshipMappingFromKeyPath:@"users"
toKeyPath:@"users"
withMapping:userMapping];
[mapping addPropertyMapping:userRelationship];