How to Store User Entered Details and Get It Back in ios?

前端 未结 3 1042
深忆病人
深忆病人 2021-01-17 06:41

How to Store user entered details and Get it Back in ios?

  • I had Following TextFields

3条回答
  •  耶瑟儿~
    2021-01-17 06:57

    If the user data is properly stored in core data, you can retrieve it with either a NSFetchedResultsController (FRC) or a simple NSFetchRequest. In both cases you would create a NSFetchRequest for your 'user' entity and assign it an NSPredicate. The predicate is defined similar to a SQL query. The NSFetchRequest will retrieve any managed objects that match the predicate.

    You might consider though passing these arguments into the second viewController as either arguments or as public properties, rather than fetching and storing them in CoreData. CoreData is great for persisting data on disk when it needs to be query-able (like and SQL database). It's also useful when the UI needs to be frequently updated to reflect changes in the persisted data. You can use NSFetchedResultsContoller to monitor changes to your data mode and automatically notify the UI layer of those changes.

    Example using an NSFetchRequest, its the simplest way to do what you are asking. It assumes you have an entity called "UserDetails" and it has a property called "user_id" to identify a specific user

    // A pointer to your main thread context, since you will be using this info to 
    // populate data on the UI. I typically use a singleton pattern to access my 
    // managed object context 
    NSManagedObjectContext *context = [DBController sharedInstance].mainManagedObjectContext;
    // Create the fetch request
        NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"UserDetails"];
    
    // Assign the predicate
            fetchRequest.predicate = [NSPredicate predicateWithFormat:@"user_id = %@",];
    
    // Execute the fetch request on the main thread managed object context
            NSError *error = nil;
            NSArray *objects = [context executeFetchRequest:fetchRequest error:&error];
        UserDetail *detail = [object firstObject];
    

提交回复
热议问题