- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 NSManagedObjectContext
的新手。我在我的应用程序中创建了一个实体 Link
,其中包含一个 NSString *url
。
在我的应用程序的某个时刻,我需要在我的基础中插入一个新的 Link
,所以我只需这样做:
Link *link = [NSEntityDescription
insertNewObjectForEntityForName:@"Link"
inManagedObjectContext:self.managedObjectContext];
link.url = myUrl;
但在执行此操作之前,我想检查我的库中是否还没有具有相同 url 的链接。我不知道该怎么做...我应该怎么做?
编辑:从我在网上找到的工具中使用此方法的基础检索数据:
// Fetch objects with a predicate
+(NSMutableArray *)searchObjectsForEntity:(NSString*)entityName withPredicate:(NSPredicate *)predicate andSortKey:(NSString*)sortKey andSortAscending:(BOOL)sortAscending andContext:(NSManagedObjectContext *)managedObjectContext
{
// Create fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
// If a predicate was specified then use it in the request
if (predicate != nil)
[request setPredicate:predicate];
// If a sort key was passed then use it in the request
if (sortKey != nil) {
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:sortAscending];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
}
// Execute the fetch request
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
// If the returned array was nil then there was an error
if (mutableFetchResults == nil)
NSLog(@"Couldn't get objects for entity %@", entityName);
// Return the results
return mutableFetchResults;
}
我想知道如何使用它。
感谢您的帮助。
最佳答案
您提供的方法只搜索与 NSManagedObjectContext 中的属性匹配的 NSManagedObject,如果存在,则返回它。
但是,您需要实现的是所谓的“查找或创建”模式,Core Data 编程指南中对此进行了讨论。基本上,您搜索符合特定条件的对象,如果存在则返回该对象。如果该对象不存在,则创建它。
例如
+ (NSString *)entityName
{
return NSStringFromClass([Link class]);
}
+ (instancetype)findUsingPredicate:(NSPredicate *)predicate withContext:(NSManagedObjectContext *)context
{
Link * entity;
// Setup the fetchRequest
NSFetchRequest * fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[[self class] entityName]];
fetchRequest.predicate = predicate;
// Credit: @Martin R
[fetchRequest setFetchLimit:1];
// Execute the fetchRequest
NSError *error = nil;
NSArray * matchingLinks = [context executeFetchRequest:fetchRequest error:&error];
// MatchingLinks will only return nil if an error has occurred otherwise it will return 0
if (!matchingLinks)
{
// handle error
// Core data returns nil if an error occured
NSLog(@"%s %@", __PRETTY_FUNCTION__, [error description]);
}
else if ([matchingLinks count] <= 0)
{
// if the count <= 0, there were no matches
NSLog(@"%s Not found", __PRETTY_FUNCTION__);
} else {
// A link with a url that matches the url in the dictionary was found.
NSLog(@"%s Found", __PRETTY_FUNCTION__);
entity = [matchingLinks lastObject];
}
return entity;
}
+ (instancetype)findUsingPredicate:(NSPredicate *)predicate orCreateWithContext:(NSManagedObjectContext *)context
{
Link * entity = [[self class] findUsingPredicate:predicate withContext:context];
if (!entity) {
entity = [[self class] createWithContext:context];
}
return entity;
}
+ (isntancetype)createWithContext:(NSManagedObjectContext *)context
{
return [[self class] alloc] initWithContext:context];
}
- (instancetype)initWithContext:(NSManagedObjectContext *)context
{
Link * entity = [NSEntityDescription insertNewObjectForEntityForName:[[self class] entityName] inManagedObjectContext:context];
return entity;
}
用例:
NSString * url = @"http://www.mylink.com";
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"url = %@", url];
Link * link = [Link findUsingPredicate:predicate orCreateWithContext:self.managedObjectContext];
link.url = url;
关于ios - 如何使用 NSManagedObjectContext 进行条件请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19819791/
我尝试为我的 UITableViewController 使用 NSFetchedResultsController(FRC),因为我喜欢 FRC 附带的功能,而不是尝试手动管理我的 UITableV
我的应用程序中目前有 2 个上下文。我的应用程序使用多个选项卡,因此一个选项卡可能正在显示数据,而另一个选项卡可能处于数据输入模式。 我使用一个主要上下文来读取大部分数据以进行显示。当我插入数据时,我
我在 appDelegate 中创建了一个主要的 NSManagedObjectContext。 现在,我正在使用另一个 NSManagedObjectContext 来编辑/添加新对象,而不会影响主
假设我们有一个应用程序需要显示地点列表并且在 3 个线程上运行: 主线程 主线程后台同步(同步有服务器的地方) 地理编码线程(对位置进行地理编码背景) 在所有 3 个线程中,我都有专用的 NSMana
所以我需要使用多线程来使用 cora-data 托管对象上下文。我发现下面的实现几乎是我所需要的。我有 2 个问题: 在哪里释放我分配的 MOC?我不想遍历应用程序中的每个函数线程并调用函数来释放 M
我正在开发一个使用 coredata 的 ipad 应用程序。它下载网络数据库上的信息,并将其记录在 coredata 中。该应用程序基于分割 View 。我的问题是在后台下载和记录数据。 这是我的做
有没有办法在主线程之外的后台保存我的 NSManagedObjectContext ?保存会减慢应用程序的执行速度,因为它通常需要大约 2 秒。 最佳答案 是的,有。 Apple recommends
在 iPhone 的核心数据中,我在尝试将数据保存到 NSManagedObjectContext 时遇到了各种错误。 我相信我的问题都与我使用在多个线程中使用的 NSManagedObjectCon
我有两个实体,每个实体都显示在自己的 UITableView 部分上。 我启用了编辑功能,允许用户通过向右滑动来删除行。这对于第一个实体来说效果很好,但是当我尝试删除第二个实体中的对象时,我收到此错误
我有以下设置: NSManagedObjectContext *parent = [[NSManagedObjectContext alloc]
在调试时,如果我有一个 NSManagedObjectContext,有没有办法查看里面有什么对象。 基本上我有一个保存错误,因为保存了一个不符合 NSCoding 的 CGColor。但我不知道这个
我正在尝试制作一个用户可以编辑 managedObject 属性的应用程序在 View 中,然后选择 Done保留更改,或 Cancel撤消更改。 为了实现此行为,我计划执行以下操作 - 当 View
最近我遇到了一个无法解释的问题。所以我有一个 DocumentContext 类,其中包含一个 Document (NSManagedObject)。这些类的代码是 extension Documen
我想在插入新数据之前清除 CoreData 实体中的所有数据。到目前为止,我尝试过删除所有内容,并且不允许我插入更新的数据,即使对这两个操作使用相同的上下文也是如此。有更好的方法吗?这是我一直在实现的
在我的应用中,我将 Core Data 堆栈从 AppDelegate 移至其自己的名为 LiftEventDataManager 的类中。在一些类中,我获得了对该堆栈的引用,当然,每次我执行操作时都
我正在使用以下代码创建一个对象: +(Checkin *) newCheckinWithId:(NSString*) checkinID forVenueId:(NSString *)venueId
我是 Core Data 的新手。 我的数据模型有一个 User 实体,它有一个属性 fullName,它是从服务器获取的。我使用 NSFetchedResultsController 在 Table
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingSt
我使用 Core Data - 我已经注册并正在监听 NSManagedObjectContextDidSaveNotification:我有一个数据集合(来自 JSON),我想要保存它,在保存所有对
我的核心数据堆栈像往常一样在 AppDelegate 中设置。我是一个优秀的 OO 公民,并认识到可以通过 [[UIApplication sharedApplication] delegate] m
我是一名优秀的程序员,十分优秀!