在 for 循环中传递数据时,MagicalRecord 正在保存 null

Posted

技术标签:

【中文标题】在 for 循环中传递数据时,MagicalRecord 正在保存 null【英文标题】:MagicalRecord is saving null when passed data inside a for loop 【发布时间】:2014-03-28 11:03:31 【问题描述】:

我正在使用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];

   ];

【问题讨论】:

你有解决办法吗?有什么线索吗?真的很奇怪! 什么都没有……我想不通。我想知道最初的 MagicalRecord 设置是否有问题。因为我遇到的另一个错误是,如果我尝试通过调用 MR_truncateAll 方法清除数据,我的应用程序也会崩溃。 您将这段代码保存在哪里? @user2939977 是的,这可能是一种可能性:您的 AppDelegate.m 中的 application:(UIApplication *)application didFinishLaunchingWithOptions: 中有 [MagicalRecord setupCoreDataStack]; 吗? “我的应用程序也崩溃了”我认为如果您提供崩溃报告会很有帮助。正如@shingo.nakanishi 已经提到的,您需要在导入后保存。 【参考方案1】:

这很正常。您只创建实体,但忘记保存它们!

我认为你可以这样做:

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]);
];

【讨论】:

【参考方案2】:

我认为你需要保存。比如saveWithBlock等等。

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) 
 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_createInContext:localContext];
        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];
    
];

【讨论】:

感谢您的回复,但我仍然看到上面代码的 (null) 值。 save: 应该在 for 循环之外。不能去save: block里面的for loop!这是肯定的。 而要保存的函数是:NSManagedObjectContext *localContext = [NSManagedObjectContext MR_defaultContext];[localContext MR_saveOnlySelfWithCompletion:^(BOOL success, NSError *error) ... ]; @user2939977 (*゚ロ゚)哦,我明白了。你的问题ExploreCD 属性总是nil,如果你保存与否。 @Gabriel.Massana 我的英语不好。我想看看你的意思。你的意思是[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) for() [ExploreCD MR_createInContext:localContext]; 是NG。

以上是关于在 for 循环中传递数据时,MagicalRecord 正在保存 null的主要内容,如果未能解决你的问题,请参考以下文章

Golang for 循环中使用goroutine

Python:无法通过`for`循环传递从文件中读取的字符串值

在for循环中传递多个参数的方法?

在传递给函数之前,for循环递增到限制

微信小程序for循环中传递动态参数

循环遍历 JSON 数组