ios 如何使用谓词从 Coredata 中获取单个对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19397194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:21:38  来源:igfitidea点击:

How to fetch a single object from Coredata using a predicate

iosobjective-ccore-datanspredicate

提问by HurkNburkS

I am trying to fetch a single object from my coredatabase, however it keeps returning null. My method is based off another method which returns every value from the coredata object that I am accessing..

我试图从我的核心数据库中获取单个对象,但它一直返回 null。我的方法基于另一种方法,该方法返回我正在访问的 coredata 对象中的每个值。

I have never tried this before and have tried reading apples documents but its just not making sense.. this is what my method looks like

我以前从未尝试过,也曾尝试阅读苹果文档,但它只是没有意义……这就是我的方法的样子

- (NSMutableArray *)readSelectedInstall:(NSString *)projIDString {
    NSManagedObjectContext *context = [self managedObjectContext];

    if (context == nil) {
        NSLog(@"Nil");
    }
    else {


        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"InstallProject" inManagedObjectContext:context];
        [fetchRequest setEntity:entity];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ProjID==%@", projIDString];
        [fetchRequest setPredicate:predicate];

        NSError *error;

        NSMutableArray *installProjectDictionaryArray = [[NSMutableArray alloc] init];


        NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
        for (InstallProject *installProj in fetchedObjects) {

            NSMutableDictionary *tempInstallProjectDictionaryArray = [[ NSMutableDictionary alloc] init];

            [tempInstallProjectDictionaryArray setObject:installProj.companyName forKey:@"CompanyName"];
            [tempInstallProjectDictionaryArray setObject:installProj.projNo forKey:@"ProjNo"];
            [tempInstallProjectDictionaryArray setObject:installProj.projID forKey:@"ProjID"];

            [installProjectDictionaryArray addObject:tempInstallProjectDictionaryArray];
        }

        return installProjectDictionaryArray;
    }
    return nil;
}

any help getting me to return a single item thats projID matches the projIDString would be greatly appreciated.

任何让我返回 projID 与 projIDString 匹配的单个项目的帮助将不胜感激。

回答by karthika

Import NSManagedObject(InstallProject) and fetch one object like this,

导入 NSManagedObject(InstallProject) 并像这样获取一个对象,

-(InstallProject *)readSelectedInstall:(NSString *)projIDString
{

    NSArray *fetchedObjects;
    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"InstallProject"  inManagedObjectContext: context];
    [fetch setEntity:entityDescription];
    [fetch setPredicate:[NSPredicate predicateWithFormat:@"(ANY ProjID contains[cd] %@)",projIDString]];
    NSError * error = nil;
    fetchedObjects = [context executeFetchRequest:fetch error:&error];

    if([fetchedObjects count] == 1)
    return [fetchedObjects objectAtIndex:0];
    else
    return nil;  

}

回答by JohnVanDijk

What about the fetchLimiton the fetchRequestYou can set that to 1

关于什么fetchLimitfetchRequest您可以设置到1

The fetch limit specifies the maximum number of objects that a request should return when executed.

获取限制指定请求在执行时应返回的最大对象数。

Eg. In Swift :

例如。在斯威夫特:

let fetchRequest = NSFetchRequest(entityName: "Cart")
fetchRequest.fetchLimit = 1
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]

Edit:The sortDescriptors are set only because I was using this fetchRequest with an NFRC, you can just execute the fetchRequest too :

编辑:sortDescriptors 的设置只是因为我将这个 fetchRequest 与 NFRC 一起使用,您也可以执行 fetchRequest :

do {
  let fetchRequest = NSFetchRequest(entityName: "Cart")
  fetchRequest.fetchLimit = 1
  fetchRequest.predicate = NSPredicate(format: "name == %@", mainCart)
  var objects: [Cart]
  try objects = mainMoc.executeFetchRequest(fetchRequest) as! [Cart]
} catch {

}