【问题标题】:iOS Magical Record not savingiOS Magical Record 不保存
【发布时间】:2014-04-12 09:53:56
【问题描述】:

所以我似乎无法在我的表格视图中显示任何数据,但后来我注意到上下文没有保存并且我一直收到此错误:

[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;
}

【问题讨论】:

  • 这似乎不是错误,它是预期的 NSLog 输出。你在重新加载你的表吗?你怎么确定它没有被保存?
  • 感谢院长的快速回复。数据没有被保留,如果我退出应用程序并返回它就消失了。如果我在这里做一些根本错误的事情,请随时指出这一点。
  • 是的,我“认为”我正在正确地重新加载它,我将编辑帖子以显示数据重新加载。
  • 您必须关闭 MR 日志记录,或者您可能没有显示所有控制台输出。你知道如何打开/关闭该选项吗?
  • 显示您在哪里创建 NSmanagedObject 并将其插入到您的上下文中。

标签: ios core-data magicalrecord


【解决方案1】:

也许您没有创建新的 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);


    }

【讨论】:

  • 谢谢你 - 我知道我错过了一些重要的东西,比如实际创建郊区哈哈,再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
  • 1970-01-01
  • 2014-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多