简体   繁体   English

核心数据/范围更新

[英]Core Data / Range Update

Here is a simple mySQL request : 这是一个简单的mySQL请求:

UPDATE myTable SET X=X-1 WHERE (X>100)

Now my question goes like this. 现在我的问题是这样的。 How would I write a similar request in Core Data ? 我将如何在Core Data中编写类似的请求?

I am starting to find out my way in Core Data; 我开始在核心数据中找到出路。 but the last part of the above SQL order gives me problems in Core Data. 但是上述SQL命令的最后一部分给了我Core Data问题。

In other word : how do I handle the WHERE (X>100) part. 换句话说:我该如何处理WHERE (X>100)部分。

If the mySQL request concerned one record only, I would use some thing like this : 如果mySQL请求仅涉及一条记录,我将使用类似以下内容:

[matchItem setValue:VALUE forKey:@"X"];

But how do I do if is a range of records, like here, where I want to perform some actions for all records where X>100. 但是,如果要记录的范围是多少(如此处所示),我想对X> 100的所有记录执行一些操作。

Use an NSPredicate and add it to your NSFetchRequest . 使用NSPredicate并将其添加到您的NSFetchRequest

NSPredicate *predicate = [NSPredicate predicateWithFormat:
    @"X > %@", [NSNumber numberWithInt:100]];

Iterate through your results array and set the value as appropriate. 遍历结果数组并适当设置值。

for (NSManagedObject *match in resultsArray) {
    NSInteger newValue = [[match objectForKey:@"X"] intValue] +1;
    [match setValue:[NSNumber numberWithInt:newValue] forKey:@"X"];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM