gpt4 book ai didi

ios - 在for循环中传递数据时,MagicalRecord将保存null

转载 作者:行者123 更新时间:2023-12-01 17:08:07 24 4
gpt4 key购买 nike

我正在使用AFNetworking来调用所有下拉数据的Web服务。数据被放入responseobject中,然后我尝试将其放入CoreData中。

由于某种原因,数据没有存储到CoreData实体中。我的代码:

    for (int i = 0; i < [[responseObject valueForKey:@"belief"] count]; i++) {

NSString * topicID = [[[[responseObject valueForKey:@"belief"] valueForKey:@"topic"] objectAtIndex:i] valueForKey:@"_id"];
NSString * title = [[[[responseObject valueForKey:@"belief"] valueForKey:@"topic"] objectAtIndex:i] valueForKey:@"title"];
NSString * body = [[[[responseObject valueForKey:@"belief"] valueForKey:@"topic"] objectAtIndex:i] valueForKey:@"body"];
NSString * category = [[[[responseObject valueForKey:@"belief"] valueForKey:@"topic"] objectAtIndex:i] valueForKey:@"category"];
NSNumber * nFor = @([[[[[responseObject valueForKey:@"belief"] valueForKey:@"topic"] objectAtIndex:i] valueForKey:@"nFor"] intValue]);

NSLog(@"Title being sent is %@",title);
NSLog(@"Number of votes sent is %@",nFor);

ExploreCD *exploreCD = [ExploreCD MR_createEntity];
exploreCD.topicID = topicID;
exploreCD.title = title;
exploreCD.body = body;
exploreCD.category = category;
exploreCD.nFor = nFor;

NSLog(@"Title being saved is %@",exploreCD.title);
NSLog(@"Number of votes saved is %@",exploreCD.nFor);

//[NSManagedObjectContext MR_contextForCurrentThread];
}

这是我的代码中的 NSLogs正在读取的内容。任何帮助将不胜感激
2014-03-28 18:49:39.677 Karmo[8328:60b] Title being sent is a odio in
2014-03-28 18:49:39.677 Karmo[8328:60b] Number of votes sent is 0
2014-03-28 18:49:39.677 Karmo[8328:60b] Title being saved is (null)
2014-03-28 18:49:39.677 Karmo[8328:60b] Number of votes saved is (null)
2014-03-28 18:49:39.678 Karmo[8328:60b] Title being sent is amet nunc viverra dapibus
2014-03-28 18:49:39.678 Karmo[8328:60b] Number of votes sent is 0
2014-03-28 18:49:39.678 Karmo[8328:60b] Title being saved is (null)
2014-03-28 18:49:39.678 Karmo[8328:60b] Number of votes saved is (null)
2014-03-28 18:49:39.678 Karmo[8328:60b] Title being sent is semper sapien a libero nam
2014-03-28 18:49:39.678 Karmo[8328:60b] Number of votes sent is 2
2014-03-28 18:49:39.678 Karmo[8328:60b] Title being saved is (null)
2014-03-28 18:49:39.678 Karmo[8328:60b] Number of votes saved is (null)
2014-03-28 18:49:39.679 Karmo[8328:60b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x109346550) NO CHANGES IN ** DEFAULT ** CONTEXT - NOT SAVING

afnetworkwing代码的成功部分中的for循环完成之后,我将用此代码保存这些新的CoreData实体。
    [MagicalRecord saveUsingCurrentThreadContextWithBlock:nil completion:^(BOOL success, NSError *error) {

exploreCellData = [[ExploreCD MR_findAll] mutableCopy];

}];

最佳答案

这是正常的。您只创建了实体,却忘记了保存它们!

我认为您可以执行以下操作:

NSManagedObjectContext * context = [NSManagedObjectContext MR_context]; // New context
for (int i = 0; i < [[responseObject valueForKey:@"belief"] count]; i++) {
NSString * topicID = [[[[responseObject valueForKey:@"belief"] valueForKey:@"topic"] objectAtIndex:i] valueForKey:@"_id"];
NSString * title = [[[[responseObject valueForKey:@"belief"] valueForKey:@"topic"] objectAtIndex:i] valueForKey:@"title"];
NSString * body = [[[[responseObject valueForKey:@"belief"] valueForKey:@"topic"] objectAtIndex:i] valueForKey:@"body"];
NSString * category = [[[[responseObject valueForKey:@"belief"] valueForKey:@"topic"] objectAtIndex:i] valueForKey:@"category"];
NSNumber * nFor = @([[[[[responseObject valueForKey:@"belief"] valueForKey:@"topic"] objectAtIndex:i] valueForKey:@"nFor"] intValue]);

NSLog(@"Title being sent is %@",title);
NSLog(@"Number of votes sent is %@",nFor);

ExploreCD *exploreCD = [ExploreCD MR_createEntityInContext:context];
exploreCD.topicID = topicID;
exploreCD.title = title;
exploreCD.body = body;
exploreCD.category = category;
exploreCD.nFor = nFor;

NSLog(@"Title being saved is %@",exploreCD.title);
NSLog(@"Number of votes saved is %@",exploreCD.nFor);
}
// Save the datas
[context MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
if (!success)
NSLog(@"Error: %@", [error localizedDescription]);
}];

关于ios - 在for循环中传递数据时,MagicalRecord将保存null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22711044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com