ios - 核心数据如何更新一条记录?
<p><p>我有一些对象是与对象用户mani-to-one相关的图像,例如图像<<--->用户。现在我想做,当用户登录时,我会为每个图像显示一个按钮以添加到收藏夹,当我单击此按钮时运行此代码:</p>
<pre><code>User * user = [ currentUser];
Image * image = (Image*)];
yacht.whoLiked = user
</code></pre>
<p>问题不是我在同一个 Controller 中,而是在 <strong>主 Controller </strong> 之前,因为我所做的是将图像的拇指加载到 Collection View 中(并在此 Controller 中从DB)然后当我按下拇指时,我进入另一个 Controller ,显示大图像和添加收藏夹的按钮,当我按下它然后回到旧 Controller 的 viewDidAppear 中的旧 Controller 时,我每次重新加载来自数据库的数据,但我看不到任何变化,如果我更改部分( Controller )并且我回来查看我看到数据更新</p>
<p>这就是我从 <strong>主 Controller </strong> 调用 Db 的方式:</p>
<pre><code>- (void)fetchImages
{
NSFetchRequest *request = ;
request.predicate = %@", self.category.name];
NSSortDescriptor *sortName = [ initWithKey:@"headline" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
request.sortDescriptors = ;
NSError * error = nil;
self.images = ;
;
}
- (void)useDocument
{
if (![ fileExistsAtPath:]) {
// CREATE
[self.database saveToURL:self.database.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
;
}];
} else if (self.database.documentState == UIDocumentStateClosed) {
// OPEN
[self.database openWithCompletionHandler:^(BOOL success) {
;
}];
} else if (self.database.documentState == UIDocumentStateNormal) {
// USE
;
}
}
- (void)setDatabase:(UIManagedDocument *)database
{
if (_database != database) {
_database = database;
;
}
}
-(void)viewDidAppear:(BOOL)animated{
;
;
//;
;
;
self.collectionView.alpha = 1;
;
}
</code></pre>
<p>为什么如果我回来并返回代码工作,否则就像我没有调用服务器刷新数组一样?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试这个代码:</p>
<pre><code>NSFetchRequest *request = [ init];
];
NSPredicate *predicate = ;
;
NSError *error = nil;
NSArray *results = ;
YourObject *object = ;
object.title = @"Bla bla bla";
//save changes
;
if (error) {
//Do something
}
</code></pre>
<p>这 2 个链接很有帮助:</p>
<p> <a href="http://developer.apple.com/library/iOS/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html" rel="noreferrer noopener nofollow">http://developer.apple.com/library/iOS/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html</a> </p>
<p> <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSFetchRequest_Class/NSFetchRequest.html" rel="noreferrer noopener nofollow">https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSFetchRequest_Class/NSFetchRequest.html</a> </p></p>
<p style="font-size: 20px;">关于ios - 核心数据如何更新一条记录?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/19520559/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/19520559/
</a>
</p>
页:
[1]