简体   繁体   English

如果两者相同,如何匹配NSUserDefaults和CoreData属性以及获取数据

[英]How To Match NSUserDefaults and CoreData Attribute and Get Data if both are same

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. 在这里,我有一个名为ClipTable的实体,我要在这里获取所有用户的所有与剪辑有关的信息,但是我想与已登录的特定用户获取剪辑信息,因为我已经使用NSUserDefaults存储了他的user_id。 And my clip table has a attribute named created_by_user, which is basically a userid of a particular user who stores the clip. 我的剪辑表具有一个名为created_by_user的属性,该属性基本上是存储剪辑的特定用户的用户ID。 So using this created_by_user attribute and user_id from NSUserDefaults i want to check whether both are same. 因此,我想使用NSUserDefaults中的created_by_user属性和user_id来检查两者是否相同。 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. 下面是我只能使用NSFetchResultController在tableView中显示所有用户的所有剪辑记录的代码。 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"] 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 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. 从currentLoggedInUser中的NSUserDefaults获取当前登录用户的值,并将其传递给下面。

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

for Category you can use AND 对于类别,您可以使用AND

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM