0

我有一个问题,通过 coredata 获取的数据然后 count 中没有记录。当应用程序第一次运行时,count 的值为零,一旦取出记录,count 的值就等于 no。的记录。现在我想将 count 的值重置为零。需要你的好意建议。

NSFetchRequest *fetchRequest = [NSFetchRequest  fetchRequestWithEntityName:@"Article"];
NSUInteger count = [self.managedObjectContext countForFetchRequest:fetchRequest error:&error];
if(count == 0)
    {
     NSArray *array = [json valueForKey:@"Artikler"];
     for(id key in array){
     Article *article = [NSEntityDescription insertNewObjectForEntityForName:@"Article" inManagedObjectContext:context];
     article.articleId = [NSNumber numberWithInt:[[key valueForKey:@"Id"]intValue]];       
            if([key valueForKey:@"Name"]== [NSNull null])
                article.name = @"Tittel";
            else
                article.name = [key valueForKey:@"Name"];
}}
4

1 回答 1

0

This is Simple Logic for you.

Add all data in NSMutableArray

If you want to count number of record in your Array then write myArray.count this return number of record that are fetched from coreData by you.

and If You want to reset Zero value the then write following code

if(myAraay.count > 0)
  [myArray removeAllObjects];

This code remove all data from array so your array has no value.

Then when you write myArray.count then it give Zero value.

于 2013-03-24T10:30:29.000 回答