Ios 将多个CoreData对象发送到MySQL数据库
尽管有一些有趣的帖子,我还是面临着一些无法理清的问题。我不知道如何将存储为核心数据对象的多个条目导出到mac上运行的MySQL数据库。对于一个条目来说一切都很好,但我不知道添加多个条目的最佳方法(一个大字典vs多个包含一个条目的字典的请求) 这是我的密码:Ios 将多个CoreData对象发送到MySQL数据库,ios,mysql,objective-c,core-data,export,Ios,Mysql,Objective C,Core Data,Export,尽管有一些有趣的帖子,我还是面临着一些无法理清的问题。我不知道如何将存储为核心数据对象的多个条目导出到mac上运行的MySQL数据库。对于一个条目来说一切都很好,但我不知道添加多个条目的最佳方法(一个大字典vs多个包含一个条目的字典的请求) 这是我的密码: NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *d
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:nil delegateQueue: [NSOperationQueue mainQueue]];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
NSArray *allPersons = [[PersonStore sharedStore] allPersons];
for (Person *element in allPersons) {
[dictionary setValue:element.firstName forKey:@"firstname"];
[dictionary setValue:element.lastName forKey:@"lastname"];
[dictionary setValue:element.dateOfBirth forKey:@"dateOfBirth"];
// ---> here I am lost. How can I append all the different Person onjects into the data to the body I am going to send in a single run? Multiple requests versus a big one?
}
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
NSString *urlString = @"http://my_name.local:8888/postPerson.php";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:data]; //set the data as the post body
[urlRequest addValue:@"postValues" forHTTPHeaderField:@"METHOD"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[urlRequest addValue:[NSString stringWithFormat:@"%lu",(unsigned long)data.length] forHTTPHeaderField:@"Content-Length"];
NSURLSessionDataTask *dataTask =[defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *dataRaw, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:dataRaw
options:kNilOptions error:&error];
NSString *result = [NSString stringWithFormat:@"%@", json];
if (error) {
UIAlertView * av = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Checking permissions:\n\nErreur:\n%@.\n", error] message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[av show];
}
UIAlertView *av = [[UIAlertView alloc] initWithTitle:result message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[av show];
}];
[dataTask resume];
}
我在代码中插入我的问题
非常感谢您的帮助!这取决于您的
posterson
php脚本能否在一篇文章中处理多人。如果需要,我可以编辑我的php脚本。我想知道是否最好有一个大字典(里面有一个由我的所有Person对象组成的数组)或者为每个Person对象创建一个字典?但我不知道如何使用iOS NSURLSession管理许多字典,而不需要多次调用该方法。我将构建一个字典数组,其中nsjsonserialization
应该能够编码为JSON。然后编写PHP脚本以正确解析JSON-应该是straight向前循环JSON中数组的所有元素。