自动生成的主键

Posted

技术标签:

【中文标题】自动生成的主键【英文标题】:Automatically Generated Primary Key 【发布时间】:2015-06-13 12:33:40 【问题描述】:

我正在开发一个 ios 应用程序。使用核心数据存储数据。但是自动生成主键。可以使用 ID 存储数据。

//Code Is

NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *userobj = [NSEntityDescription insertNewObjectForEntityForName:@"DetailSave" inManagedObjectContext:context];

    [userobj setValue:[NSNumber numberWithInteger:_userObj.userId] forKey:@"id"];
    [userobj setValue:_titleLabel.text forKey:@"title"];
    NSInteger valInt = 1;
    NSNumber *val = [NSNumber numberWithInteger:valInt];
    [userobj setValue:val forKey:@"quantity"];

    NSString *maxpurch = [dict objectForKey:@"_max_purchases_per_user"];
    NSNumber *maxval = [NSNumber numberWithInteger:[maxpurch integerValue]];
    [userobj setValue:maxval forKey:@"max_purch"];

    NSString *maxPurch = [dict objectForKey:@"_max_purchases"];
    NSNumber *max = [NSNumber numberWithInteger:[maxPurch integerValue]];
    [userobj setValue:max forKey:@"available"];

    NSString *baseprice = [dict objectForKey:@"_base_price"];
    NSNumber *basePrice = [NSNumber numberWithInteger:[baseprice integerValue]];
    [userobj setValue:basePrice forKey:@"amount"];

    //NSLog(@"%@",userobj);
       NSLog(@"app dir: %@",[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);

    NSError *error = nil;
    if (![context save:&error]) 
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    

    NSFetchRequest *request = [[NSFetchRequest alloc] init];       // Count Entity
    [request setEntity:[NSEntityDescription entityForName:@"DetailSave" inManagedObjectContext:context]];
    [request setIncludesSubentities:NO];
    NSError *err;
    NSUInteger count = [context countForFetchRequest:request error:&err];
    if(count == NSNotFound) 

    

【问题讨论】:

***.com/questions/10189696/… 【参考方案1】:

如果 M=8,或者即使 M 接近 8(例如 9 或 10),那么这需要一段时间,你可以更聪明。

-(NSMutableArray *)getEightRandomLessThan:(int)M 
  NSMutableArray *listOfNumbers = [[NSMutableArray alloc] init];
  for (int i=0 ; i<M ; ++i) 
    [listOfNumbers addObject:[NSNumber numberWithInt:i]]; // ADD 1 TO GET NUMBERS BETWEEN 1 AND M RATHER THAN 0 and M-1
  
  NSMutableArray *uniqueNumbers = [[[NSMutableArray alloc] init] autorelease];
  int r;
  while ([uniqueNumbers count] < 8) 
    r = arc4random() % [listOfNumbers count];
    if (![uniqueNumbers containsObject:[listOfNumbers objectAtIndex:r]]) 
      [uniqueNumbers addObject:[listOfNumbers objectAtIndex:r]];
    
  
  [listOfNumbers release];
  return uniqueNumbers;

unsigned int permuteQPR(unsigned int x)

    static const unsigned int prime = 4294967291;
    if (x >= prime)
        return x;  // The 5 integers out of range are mapped to themselves.
    unsigned int residue = ((unsigned long long) x * x) % prime;
    return (x <= prime / 2) ? residue : prime - residue;

【讨论】:

以上是关于自动生成的主键的主要内容,如果未能解决你的问题,请参考以下文章

mybatis 中如何获取自动生成的主键

Hibernate 手动和自动生成的主键

mysql的主键是自动增长的,oracle的主键是起啥作用的

Hibernate的主键生成策略的介绍

使用自动生成的主键 PK 的核心数据

在 SQL 脚本中引用生成的主键