避免在 ios 中的 UITableView 中出现重复条目

Posted

技术标签:

【中文标题】避免在 ios 中的 UITableView 中出现重复条目【英文标题】:Avoid duplicate entries in a UITableView in ios 【发布时间】:2012-03-07 11:36:09 【问题描述】:

我开发了一个应用程序,其中我有一个使用核心数据框架的托管对象上下文将其内容加载到数据库的方法。我在委托的完成启动方法中调用该方法。我正在使用 UITableView 来获取数据并将其显示在表格视图中。我的问题是相同的数据在表格视图中显示两次。 当我使用 Xcode 启动应用程序时,数据只显示一次,然后在停止 xcode 之后,当我在设备中第一次打开应用程序时,我得到了相同数据的副本。我不知道如何避免这种重复,请帮助我。

    -(void) devicedetails
      
         devicename = [UIDevice currentDevice].name;
       osversion = [UIDevice currentDevice].systemVersion;
       //some other data like this
       NSManagedObjectContext context = [self managedObjectContext];
      Deviceinfo *detail = [NSEntityDescription insertNewObjectForEntityForName:@"Deviceinfo" inManagedObjectContext:nscontext];
      detail.platform = devicename;
      detail.os_version = devosversion;
       .
       .
       .
    

这是我的方法,我在下面的 didfinish 启动方法中调用它

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
   
    [self devicedetails];
    return YES;
  

谢谢

【问题讨论】:

我认为启动有问题,因为它在设备中启动时只复制一次 【参考方案1】:

每次调用 -insertNewObjectForEntityForName:inManagedObjectContext: 时,您都会插入一个新对象。

您应该做的是在插入之前尝试获取对象。如果 fetch 为空,则插入新对象,否则不插入。

【讨论】:

以上是关于避免在 ios 中的 UITableView 中出现重复条目的主要内容,如果未能解决你的问题,请参考以下文章