iOS 魔法记录不保存

标签 ios core-data magicalrecord

所以我似乎无法在我的 tableview 中显示任何数据,但后来我注意到上下文没有保存,我不断收到此错误:

[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x166a70b0) NO CHANGES IN ** DEFAULT ** CONTEXT - NOT SAVING
2014-04-12 19:49:04.722 0DataCollector[3218:60b] Managedcontext working (
    TestSuburb
)



- (IBAction)backToSuburbsTableViewController:(UIStoryboardSegue *)segue {

    if ([segue.sourceViewController isKindOfClass:[NewSuburbTableViewController class]]) {
        NSLog(@"Coming back from NewSuburbTableViewController");
        NewSuburbTableViewController *srcVC = segue.sourceViewController;
        suburbString = srcVC.suburbTextField.text;
        [suburbsArray addObject:suburbString];

        //Save Managed Object Context
        [[NSManagedObjectContext defaultContext] saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
            if (success) {
                NSLog(@"You successfully saved your context.");
            } else if (error) {
                NSLog(@"Error saving context: %@", error.description);
            }
        }];        NSLog(@"Managedcontext working %@", suburbsArray);


    }

如果您能提供任何帮助,我将不胜感激,我也尝试过保存“contextWithinCurrentThread”,但没有帮助。

TableViewController 的其余代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    suburbsArray = [[NSMutableArray alloc] init];

    [self fetchSuburbs];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)fetchSuburbs {
    //Fetch suburbs
    self.suburbsArray = [NSMutableArray arrayWithArray:[Suburb findAllSortedBy:@"name" ascending:YES]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self fetchSuburbs];

    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [suburbsArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSMutableArray *reversedSuburbsArray = [[[suburbsArray reverseObjectEnumerator] allObjects] mutableCopy];

    //Fetch suburb
    Suburb *suburb = [self.suburbsArray objectAtIndex:indexPath.row];

    // Configure the cell...
    cell.textLabel.text = suburb.name;

    return cell;
}

最佳答案

也许您没有创建新的 NSmanagedObject“郊区”并将其插入到上下文中,因此没有变化。在这种情况下,您的问题不在于保存,没有什么可保存的。

添加到可变数组并不会创建新的郊区。您只创建了一个与任何 NSmanagedObject 无关的字符串对象。

然后我建议:

- (IBAction)backToSuburbsTableViewController:(UIStoryboardSegue *)segue {

    if ([segue.sourceViewController isKindOfClass:[NewSuburbTableViewController class]]) {
        NSLog(@"Coming back from NewSuburbTableViewController");
        NewSuburbTableViewController *srcVC = segue.sourceViewController;
        suburbString = srcVC.suburbTextField.text;
        [suburbsArray addObject:suburbString];

        // Create the new suburb
        Suburb *theNewBurb = [Suburb createEntity];
        theNewBurb.burbName = suburbString; // I don't know what properties you have on Suburb so you would need to correct this
        // You also need to set any other required properties of Suburb prior to save or it will fail

        //Save Managed Object Context
        [[NSManagedObjectContext defaultContext] saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
            if (success) {
                NSLog(@"You successfully saved your context.");
            } else if (error) {
                NSLog(@"Error saving context: %@", error.description);
            }
        }];        NSLog(@"Managedcontext working %@", suburbsArray);


    }

关于iOS 魔法记录不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23028808/

相关文章:

c# - Xamarin Forms - 将 scrollview.Scrolled 附加到函数

ios - app进入applicationDidEnterBackground后,我们应该怎么做才能运行NSoperationqueue?

javascript - 浏览器滚动事件在 iOS 和 Android 设备上触发不够频繁

swift - 我如何在 Core Data 中使用一对多?

ios - 创建单独的 NSManagedObjectContext 时出错

cocoa - 静态库中的 MagicalRecord : how do I load the data model?

ios - 顶部和底部布局指南的自动布局问题

ios - WatchKit 和应用程序之间核心数据实时同步

ios - 如何从 UITableView swift 4 中删除一行,其中使用 CoreData 存储数据?

objective-c - 在 MagicalRecord 中使用现有的 SQLite 数据库