Objective c Can';t更新iOS NSManagedObject属性

Objective c Can';t更新iOS NSManagedObject属性,objective-c,ios,core-data,Objective C,Ios,Core Data,我正在开发一个note应用程序,核心数据有点问题。我遇到的问题是,在应用程序终止后,我的一个NSManagedObject对象属性(NSString)没有得到更新。场景:(1)构建并运行应用程序,(2)编写内容(UITextView),(3)按主页按钮(后台),(5)终止应用程序,(6)打开应用程序,文本在那里,(7)编辑文本(或删除文本),(8)按主页按钮,(9)终止应用程序,(10)再次打开应用程序,新文本不在那里 我想我已将错误定位到此代码段/方法: // Helper method to

我正在开发一个note应用程序,核心数据有点问题。我遇到的问题是,在应用程序终止后,我的一个NSManagedObject对象属性(NSString)没有得到更新。场景:(1)构建并运行应用程序,(2)编写内容(UITextView),(3)按主页按钮(后台),(5)终止应用程序,(6)打开应用程序,文本在那里,(7)编辑文本(或删除文本),(8)按主页按钮,(9)终止应用程序,(10)再次打开应用程序,新文本不在那里

我想我已将错误定位到此代码段/方法:

// Helper method to change page, it takes one argument, tag of the next/prevoius page.
// This method is called in viewWillAppear.
- (void)goToPage:(int)tag
{
    // Set up the new current page. 
    currentPage = [pageList objectAtIndex:tag];
    NSLog(@"%@", currentPage.pageText); // Returns the old value, not the updated. 
    if ([currentPage.inputType isEqualToString:@"drawing"]) {
        [pageView changeToDrawing];
        toolSegment.selectedSegmentIndex = 0;

        UIImage * pageImage = [UIImage imageWithData:currentPage.pageImage];
        pageView.canvas.image = pageImage;
    } else if ([currentPage.inputType isEqualToString:@"text"]) {
        [pageView changeToText];
        toolSegment.selectedSegmentIndex = 1;

        pageView.pageText.text = currentPage.pageText;
        if (editing) {
            [undoButton removeFromSuperview];
            [redoButton removeFromSuperview];
        }
    } 
    [self updateMenu];
}
当我运行
NSLog(@“%@”,currentPage.pageText)时显示该值尚未更新/保存

我这样设置值:

- (void)textViewDidEndEditing:(UITextView *)textView
{
    currentPage.pageText = textView.text;
}
// Sets the current page to the last open page
currentPageTag =  pagesNoteblock.lastOpenPage;
currentPage = [pageList objectAtIndex:currentPageTag];
NSString * currentInputType;
if (currentPage.inputType == @"drawing") {
    currentInputType = @"drawing";
} else if ([currentPage.inputType isEqualToString:@"text"]) {
    currentInputType = @"text";
}

// Sets the current page.
Page * newPage = [NSEntityDescription insertNewObjectForEntityForName:@"Page" inManagedObjectContext:moc];
newPage.createdAt = [NSDate date];
newPage.inputType = currentInputType;

// Adds the new page to the database and to the pageList array.
[pagesNoteblock addNoteblockPagesObject:newPage];
[pageList addObject:newPage];

// Sets the current page and clears the textView.
currentPage = [pageList objectAtIndex:currentPageTag];
我最初将当前页面设置为:

- (void)textViewDidEndEditing:(UITextView *)textView
{
    currentPage.pageText = textView.text;
}
// Sets the current page to the last open page
currentPageTag =  pagesNoteblock.lastOpenPage;
currentPage = [pageList objectAtIndex:currentPageTag];
NSString * currentInputType;
if (currentPage.inputType == @"drawing") {
    currentInputType = @"drawing";
} else if ([currentPage.inputType isEqualToString:@"text"]) {
    currentInputType = @"text";
}

// Sets the current page.
Page * newPage = [NSEntityDescription insertNewObjectForEntityForName:@"Page" inManagedObjectContext:moc];
newPage.createdAt = [NSDate date];
newPage.inputType = currentInputType;

// Adds the new page to the database and to the pageList array.
[pagesNoteblock addNoteblockPagesObject:newPage];
[pageList addObject:newPage];

// Sets the current page and clears the textView.
currentPage = [pageList objectAtIndex:currentPageTag];
并创建如下新页面:

- (void)textViewDidEndEditing:(UITextView *)textView
{
    currentPage.pageText = textView.text;
}
// Sets the current page to the last open page
currentPageTag =  pagesNoteblock.lastOpenPage;
currentPage = [pageList objectAtIndex:currentPageTag];
NSString * currentInputType;
if (currentPage.inputType == @"drawing") {
    currentInputType = @"drawing";
} else if ([currentPage.inputType isEqualToString:@"text"]) {
    currentInputType = @"text";
}

// Sets the current page.
Page * newPage = [NSEntityDescription insertNewObjectForEntityForName:@"Page" inManagedObjectContext:moc];
newPage.createdAt = [NSDate date];
newPage.inputType = currentInputType;

// Adds the new page to the database and to the pageList array.
[pagesNoteblock addNoteblockPagesObject:newPage];
[pageList addObject:newPage];

// Sets the current page and clears the textView.
currentPage = [pageList objectAtIndex:currentPageTag];
我还有一个助手方法可以保存页面内容,这在ViewWilldisPears和其他一些地方调用:

// Method that saves the current page's image to the database.
- (void)savePageContent
{
    // Saves the image to the database.
    if ([currentPage.inputType isEqualToString:@"text"]) {
        currentPage.pageText = pageView.pageText.text;
    } else if ([currentPage.inputType isEqualToString:@"drawing"]) {
        NSData * coreDataImage = [NSData dataWithData:UIImagePNGRepresentation(pageView.canvas.image)];
        currentPage.pageImage = coreDataImage;
    }
    pagesNoteblock.lastOpenPage = currentPageTag;
}
如有任何帮助/提示,将不胜感激


更新:小说明,似乎“页面文本”对象只能写一次,也就是说,我只能在第一次创建时对其进行“可保存”更改

Hmmm您似乎从未在NSManagedObjectContext上调用过save,在您终止应用程序后,您的对象是否仍会出现?他们在储蓄吗

在插入和删除之后,类似的操作应该会起作用:

NSError *error;
[self.context save:&error];

self.context
无论您的NSManagedObjectContext是什么。

我有两个想法,但不太确定:

  • 您确定吗,当您点击主页按钮时,会调用
    文本ViewDiEndediting
  • 我怀疑下面的方法正在做你想做的事情

    Page*newPage=[NSEntityDescription insertNewObjectForEntityForName:@“Page”在托管对象上下文:moc中]

  • 此方法为Page类插入(或创建)一个新实例/记录。它不会更新任何已经存在的记录!我看不到您的获取请求,但是当您从数据库获取时,您会得到一个数组,我假设您只显示第一个数组(atIndex:0)


    换句话说,您正在将其保存,但将其作为新记录保存在数据库列表的末尾。您没有更新已经显示的视图。

    您好,我在我的应用程序委派/控制器、appWillResignActive和appWillTerminate(或它们被称为(现在不在家)中进行保存。我是否也需要在我的其他视图控制器中保存?此问题仅出现在文本视图中,应用程序还支持“绘图”,并按应保存/更新。因此,我怀疑这个问题与储蓄无关。是和否。当我按下“完成”按钮,考虑将其移动到
    textViewDidChange
    时,它会被保存下来,但它仍然可以工作。2.这是一个非常好的观点,但是我在我的create page方法中使用了一个NSlog,在这个方法中插入的代码是存在的,并且只有在我需要的时候才会调用它。在这个方法中,我失去了一些头发。和往常一样,这是一个愚蠢的错误。在我的定制NSManagedObject中,我将文本属性实现为
    @syntezise
    ,而它应该是
    @dynamic
    。不管怎样,谢谢你@Canopus和@gamBit