gpt4 book ai didi

ios - 如果两者相同,如何匹配 NSUserDefaults 和 CoreData 属性并获取数据

转载 作者:行者123 更新时间:2023-11-29 00:50:07 25 4
gpt4 key购买 nike

这里我有一个名为 ClipTable 的实体,我在这里获取所有用户的所有剪辑相关信息,但我想获取登录的特定用户的剪辑信息,为此我使用 NSUserDefaults 存储了他的 user_id。我的剪辑表有一个名为 created_by_user 的属性,它基本上是存储剪辑的特定用户的用户 ID。因此,我想使用 NSUserDefaults 中的 created_by_user 属性和 user_id 检查两者是否相同。如果两者相同,则仅获取该特定用户的剪辑记录。下面是我只能使用 NSFetchResultController 在 tableView 中显示所有用户的所有剪辑记录的代码。但现在我只想获取特定用户的记录。很难做到这一点。感谢您的帮助。

user_id 是 [NSUserDefaults standardUserDefaults] stringForKey:@"userID"]

-(void)fetch:(NSString*)catgeory :(BOOL)All{

AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
self.managedObjectContext = delegate.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"ClipTable"];

// Add Sort Descriptors
[fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"modifieddate" ascending:NO]]];

//_fetchedResultsController=nil;
// Initialize Fetched Results Controller
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

if (All==NO) {
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(page_categorisation == %@)",catgeory]];
}

[fetchRequest setFetchBatchSize:0];
// [fetchRequest setFetchLimit:10];
// Configure Fetched Results Controller
[self.fetchedResultsController setDelegate:self];

// Perform Fetch
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];

if (error) {
NSLog(@"Unable to perform fetch.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
}

最佳答案

这是简单的解决方案,只需使用谓词来获取登录用户的剪辑。从 currentLoggedInUser 中的 NSUserDefaults 获取当前登录用户的值,并在下面传递。

NSString *predicateFrmt = @"created_by_user == %@ ;
NSPredicate *predicate = [NSPredicate predicateWithFormat: predicateFrmt, currentLoggedInUser];
fetchRequest.predicate = predicate;

对于类别,您可以使用 AND

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(page_categorisation == %@ AND created_by_user == %@ )",catgeory,currentLoggedInUser]];

关于ios - 如果两者相同,如何匹配 NSUserDefaults 和 CoreData 属性并获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38221415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com