如果两者相同,如何匹配NSUserDefaults和CoreData属性以及获取数据(How To Match NSUserDefaults and CoreData Attribute and Ge

编程入门 行业动态 更新时间:2024-10-24 04:40:51
如果两者相同,如何匹配NSUserDefaults和CoreData属性以及获取数据(How To Match NSUserDefaults and CoreData Attribute and Get Data if both are same)

这里我有一个名为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); } }

Here i have an entity named ClipTable i am fetching all clip related information of all users here but i want to fetch clip information with particular user who is logged in,for that i have stored his user_id using NSUserDefaults. And my clip table has a attribute named created_by_user, which is basically a userid of a particular user who stores the clip. So using this created_by_user attribute and user_id from NSUserDefaults i want to check whether both are same. And if both are same get the clip record of that particular user only. below is the code with which i am only able to show all the clip record of all the user in tableView using NSFetchResultController. But now i want to get records for the particular user only. Hard time doing this. Any help is appreciated.

user_id is [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]];

Here is the simple solution just use predicate to fetch the clips of logged in user. Get the value of current logged in user from NSUserDefaults in currentLoggedInUser and pass it below.

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

for Category you can use AND

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

更多推荐

本文发布于:2023-07-15 05:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110643.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   数据   CoreData   NSUserDefaults   Attribute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!