English 中文(简体)
检查核心数据条目是否存在核心数据条目,并代之以一个独有的弹进值 c
原标题:checking the existance of an entry in the core data and replacing it with a unique value in bjective c

我的一个应用程序中有一个核心数据。 所有的数据都来自服务器。 在一个点上我必须检查服务器上的数据是否已经存在于核心数据中。 如果它已经存在, 只需替换它。 检查可以用我对每个条目的独特值来完成。 如何实现这个目标? 这是我所做的:

` for(int i=0;i<[ar count];i++){
            NSFetchRequest * request = [[NSFetchRequest alloc] init];
         NSManagedObjectContext *context=[Server serverInfo].context;

            [request setEntity:[NSEntityDescription entityForName:@"Coupon" inManagedObjectContext:context]];
            [request setPredicate:[NSPredicate predicateWithFormat:@"upc_code = %@",[ [ar objectAtIndex:i]objectForKey:@"upc_code"]]];
            NSUInteger count = [context countForFetchRequest:request error:nil];
            [request release];
          //  [context ]

        if (count > 0){
            NSLog(@"found");} 

这里是从服务器获取的数组。 上下文是用户有时可以在服务器的条目中进行编辑。 我需要服务器中与设备同步的所有数据 。

最佳回答

不要做 count ForFetch Request , 代之以 excuteFetch request: error: , 这将返回一个有结果的数组。 这个数组的结果应该为 0 或 1, 超过 1 将会不一致, 因为我认为您的 upc_code 是独一无二的 。

如果您有 0 结果,请在上下文中创建一个新对象并设置其属性

如果结果为 1, 只需更新您通过执行FetchRequest:error 返回的该对象的属性:

最终拯救你的背景 和你的所作所为

问题回答

暂无回答




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签