改变 NSArray 中的 ManagedObjects
我有一个名为“作业”的实体,具有两个名为“已完成”和“已记录”的布尔属性。 我正在尝试检索应用程序启动时尚未记录的所有已完成作业并将其更改为已记录。我可以使用此 fetchRequest 获取所有已完成但未记录的作业:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(completed == %@ && logged == %@)", [NSNumber numberWithBool:YES], [NSNumber numberWithBool:NO]];
然后,我将此谓词分配给 fetchRequest 并调用 [managementObjectContextexecuteFetchRequest:fetchRequest] 方法来获取满足此条件的所有作业实体的数组。这似乎工作正常并且返回正确数量的作业。
我一直在尝试做的是循环返回的 NSArray,将记录的属性设置为 YES,然后保存。这似乎已完成并且不会返回任何错误,但应用程序退出时更改不会保留。我哪里出错了?
[fetchRequest setPredicate:predicate];
NSError error;
NSArray jobsToLog = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
if ([jobsToLog count] > 0) {
for (int i = 0; i < [jobsToLog count] - 1; i++) {
[[jobsToLog objectAtIndex:i] setLogged:[NSNumber numberWithBool:YES]];
// Commit the changes made to disk
error = nil;
if (![managedObjectContext save:&error]) {
// An error occurred
}
}
}
感谢期待,
I have an entity called 'Job' with two boolean attributes named 'completed' and 'logged'.
I am trying to retrieve all completed jobs that have not been logged at app start-up and change them to logged. I'm able to get all the completed but unlogged jobs with this fetchRequest:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(completed == %@ && logged == %@)", [NSNumber numberWithBool:YES], [NSNumber numberWithBool:NO]];
I'm then assigning this predicate to a fetchRequest and calling the [managedObjectContext executeFetchRequest:fetchRequest] method to get an array of all Job entities that meet this criteria. This seems to work fine and is returning the correct number of jobs.
What I've been trying to do is loop through the NSArray returned, set the logged attribute to YES and then save. This seems to complete and doesn't return any errors but the changes are not persisted when the application quits. Where am I going wrong?
[fetchRequest setPredicate:predicate];
NSError error;
NSArray jobsToLog = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
if ([jobsToLog count] > 0) {
for (int i = 0; i < [jobsToLog count] - 1; i++) {
[[jobsToLog objectAtIndex:i] setLogged:[NSNumber numberWithBool:YES]];
// Commit the changes made to disk
error = nil;
if (![managedObjectContext save:&error]) {
// An error occurred
}
}
}
Thanks in anticipation,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,您可以通过以下方式清理一些内容:
error
为 nil 并作为指针(您的声明不是)开始。如果不这样做,那么该指针的初始状态是未定义的,并且绝对不是零。First, you can clean things up a bit via:
error
being nil and being a pointer (your declaration was not). If you do not then the initial state of that pointer is undefined and definitely not nil.一些可能有帮助的注释:
for 循环从 0 循环到比计数少 1。这将跳过最后一个作业。如果只有 1 个工作,什么也不会发生。将循环更改为:
或
当从 NSArray 中提取对象时,编译器将不知道其类型。您应该显式转换对象:
您可以使用快速枚举来执行上述两项操作:
Some notes that may help:
The for loop is looping from 0 to one less than the count. This will skip the last job. If there is only 1 job, nothing will happen. Change the loop to:
or
When pulling objects out of NSArray the compiler will not know its type. You should explicitly cast the object:
You can do both of the above using fast enumeration:
如果您使用 KVC(键值编码)方法怎么办:
您的自定义 NSManagedObject 类中有 setLogged 方法吗?
What if you use the KVC (Key Value Coding) method:
Do you have a setLogged method in your custom NSManagedObject class?
我不是这里的专家,但我认为您需要通过使用 setPrimitiveValue: 告诉托管对象上下文来进行更改。所以试试这个。
I'm no expert here but I think you need to tell the managed object context by using setPrimitiveValue: to make the changes. So try this.