×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Objective-C
Posted by: Black Light
Added: Mar 3, 2013 12:47 PM
Views: 32
Tags: no tags
truncate, get_next_id (for auto_increment purpose)
  1.  
  2. + (void) truncateMyTable
  3. {
  4.     AppDelegate *appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  5.    
  6.     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  7.     NSEntityDescription *entity = [NSEntityDescription entityForName:kMyTableName inManagedObjectContext:appDelegate.managedObjectContext];
  8.     [fetchRequest setEntity:entity];
  9.    
  10.     NSError *error = nil;
  11.     NSArray *fetchedObjects = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
  12.     if (fetchedObjects == nil)
  13.     {
  14.         // error handling
  15.     }
  16.     else
  17.     {
  18.         for(int i=0;i<[fetchedObjects count];i++)
  19.         {
  20.             MyRecord *record=[fetchedObjects objectAtIndex:i];
  21.             [appDelegate.managedObjectContext deleteObject:record];
  22.         }
  23.        
  24.         [appDelegate saveContext];
  25.     }
  26. }
  27.  
  28. ////////////////////////////////////////////////////////////////
  29.  
  30. - (NSNumber*) getNextId
  31.  {
  32.  
  33.  AppDelegate *appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  34.  
  35.  NSNumber *nextId=[NSNumber numberWithInt:1];
  36.  
  37.  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  38.  NSEntityDescription *entity = [NSEntityDescription entityForName:kMyTableName inManagedObjectContext:appDelegate.managedObjectContext];
  39.  [fetchRequest setEntity:entity];
  40.  [fetchRequest setResultType:NSDictionaryResultType];
  41.  
  42.  NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"db_id"];
  43.  NSExpression *maxIdExpression = [NSExpression expressionForFunction:@"max:"
  44.  arguments:[NSArray arrayWithObject:keyPathExpression]];
  45.  
  46.  
  47.  NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
  48.  [expressionDescription setName:@"maxId"];
  49.  [expressionDescription setExpression:maxIdExpression];
  50.  [expressionDescription setExpressionResultType:NSDecimalAttributeType];
  51.  
  52.  [fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];
  53.  
  54.  NSError *error = nil;
  55.  NSArray *fetchedObjects = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
  56.  if (fetchedObjects == nil) {
  57.  
  58.  NSLog(@"error getting max id");
  59.  
  60.  }
  61.  else
  62.  {
  63.  NSNumber *t=(NSNumber*)[[fetchedObjects lastObject] objectForKey:@"maxId"];
  64.  if([t intValue]>[nextId intValue])
  65.  nextId=t;
  66.  }
  67.  
  68.  
  69.  [fetchRequest release];
  70.  
  71.  return nextId;
  72.  }
  73.  
  74.  
  75.