从 CoreData 信息 POST JSON 到服务器
Posted
技术标签:
【中文标题】从 CoreData 信息 POST JSON 到服务器【英文标题】:POST JSON to Server from CoreData Information 【发布时间】:2014-10-16 20:32:10 【问题描述】:我对 ios 开发有点陌生,所以我试图弄清楚将 JSON 字符串发布到网络服务器并取回一些信息。在第一页我要求用户给我他们的名字和姓氏。然后我为它们生成一个 ID 并将它们传递到第二页。当第二个页面加载时,我想向服务器发送一个 JSON 字符串,告诉它用户的名字、姓氏等。请求如下:
"user":
"first_name":"Joe",
"last_name":"User",
"device_descriptor":"iPhone",
"idfa":"12345678",
"btmacid":"01:23:45:67:89:ab"
创建响应成功返回如下:
"id": "8",
"first_name: "Joe",
"last_name": "User",
"device_descriptor": "iPhone",
"created_at": "2014-10-07T05:25:36.119Z",
"updated_at": "2014-10-07T05:25:36.119Z",
"idfa": "12345678",
"btmacid": "01:23:45:67:89:ab"
我目前的代码如下:
#import "WelcomeViewController.h"
#import "CoreDataStack.h"
#import "UserInformation.h"
#import <CoreData/CoreData.h>
#import <RestKit/RestKit.h>
@interface WelcomeViewController ()
@property (nonatomic,strong) NSFetchedResultsController *fetchResultsController;
@property (strong, nonatomic) IBOutlet UILabel *welcomeTextLabel;
@end
@implementation WelcomeViewController
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.fetchResultsController performFetch:nil];
UserInformation *entry = [[self.fetchResultsController fetchedObjects] objectAtIndex:0];
_welcomeTextLabel.text = [NSString stringWithFormat: @"Welcome " @"%@!", entry.firstName];
[self postUserInformation];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (NSFetchRequest *)entryFetchRequest
NSFetchRequest *fetchReqeust = [NSFetchRequest fetchRequestWithEntityName:@"UserInformation"];
fetchReqeust.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:NO]];
return fetchReqeust;
- (NSFetchedResultsController *)fetchResultsController
if (_fetchResultsController != nil)
return _fetchResultsController;
CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
NSFetchRequest *fetchRequest = [self entryFetchRequest];
_fetchResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:coreDataStack.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
return _fetchResultsController;
- (void) postUserInformation
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
[self.fetchResultsController performFetch:nil];
UserInformation *entry = [[self.fetchResultsController fetchedObjects] objectAtIndex:0];
RKObjectManager *manager = [RKObjectManager sharedManager];
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[UserInformation class]];
NSDictionary *userInformation = @
@"id" : @"userID",
@"first_name" : @"firstName",
@"last_name" : @"lastName",
@"device_descriptor" : @"deviceHardwareID",
@"created_at" : @"created_at",
@"updated_at" : @"updated_at",
@"idfa" : @"deviceAdvertisementID",
@"btmacid" : @"btmac_Id"
;
[responseMapping addAttributeMappingsFromDictionary:userInformation];
manager.requestSerializationMIMEType = RKMIMETypeJSON;
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:@"/users" keyPath:nil statusCodes:statusCodes];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:userInformation];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[userInformation class] rootKeyPath:nil method:RKRequestMethodAny];
[manager addResponseDescriptor:responseDescriptor];
[manager addRequestDescriptor:requestDescriptor];
// NSDictionary *params = @
// @"id" : entry.userID,
// @"first_name" : entry.firstName,
// @"last_name" : entry.lastName,
// @"device_descriptor" : entry.deviceHardwareID,
// @"created_at" : entry.createdAt,
// @"updated_at" : entry.updatedAt,
// @"idfa" : entry.deviceAdvertisementID,
// @"btmacid" : entry.btmacID
// ;
[manager postObject:userInformation path:@"http://serverendpoint.com/users" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
NSLog(@"Success");
failure:^(RKObjectRequestOperation *operation, NSError *error)
NSLog(@"Failure: %@", error);
];
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
*/
@end
您能提供的任何帮助将不胜感激。
【问题讨论】:
【参考方案1】:您不应该使用
创建请求映射 [requestMapping addAttributeMappingsFromDictionary:userInformation];
因为输入和输出是错误的。相反,请使用响应映射中的 inverseMapping
。
【讨论】:
以上是关于从 CoreData 信息 POST JSON 到服务器的主要内容,如果未能解决你的问题,请参考以下文章
在后台下载数据的同时,从 JSON 中下载数据并存储到 CoreData 和 Display
从 CoreData 在 NSDictionary 中制作嵌套 JSON