5

मैं कोशिश कर रहा हूँ उपयोग CoreData, लेकिन जब मैं इसे अपने परियोजना के लिए जोड़ मैं केवल दो नए तरीकों मिलती है:एक्सकोड 8 में कोरडाटा का उपयोग कैसे करें?

- (NSPersistentContainer *)persistentContainer 

और

- (void)saveContext 

अब मैं पुराने तरीकों CoreData के साथ काम करने नहीं मिल सकता है , और मुझे इन नए तरीकों और उद्देश्य-सी के साथ कोई ट्यूटोरियल नहीं मिल रहा है। मैं उद्देश्य-सी के साथ एक्सकोड 8 में persistentContainer का उपयोग करके CoreData से डेटा कैसे सहेज सकता हूं और प्राप्त कर सकता हूं?

+0

निखिल Manapure। धन्यवाद, लेकिन मैं अभी तक स्विफ्ट को समझ में नहीं आता। लेकिन यह बहुत अच्छा होगा अगर आप मुझे दिखाएंगे कि स्विफ्ट में यह कैसे कर सकता है और मैं इसे उद्देश्य-सी – scorpio

+0

@ निखिल मैनप्योर में बहुत कुछ करने की कोशिश करूंगा !!! मैं उद्देश्य सी – scorpio

+0

के साथ समाधान का इंतजार करूँगा कृपया निम्नलिखित यूआरएल से कोड देखें। https://github.com/dilipkosuri/CoreDataIOS –

उत्तर

15

आप संदर्भ प्राप्त कर सकते हैं के रूप में -

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 

या में के रूप में ऑब्जेक्टिव-सी

NSManagedObjectContext *context = ((AppDelegate*)[[UIApplication sharedApplication] delegate]).persistentContainer.viewContext; 

और डेटा की तरह लाने -

var resultArray = try self.context.fetch(EntityName.fetchRequest()) 

या उद्देश्य के रूप में -सी

NSFetchRequest<EntityName *> *fetchRequest = [EntityName fetchRequest]; 
NSError *error ; 
NSArray *resultArray= [context executeFetchRequest:fetchRequest error:&error]; 

और छँटाई के साथ डेटा लाने -

var resultArray = [EntityName]() 
do { 
     let request : NSFetchRequest<EntityName> = EntityName.fetchRequest() 
     let sortDescriptor = NSSortDescriptor(key: "somekey", ascending: true) 
     let sortDescriptors = [sortDescriptor] 
     request.sortDescriptors = sortDescriptors 
     resultArray = try self.context.fetch(request) 
} catch { 
     print("Error") 
} 

या में के रूप में ऑब्जेक्टिव-सी

NSFetchRequest<EntityName *> *fetchRequest = [EntityName fetchRequest]; 
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"someKey" ascending:YES]; 
fetchRequest.sortDescriptors = @[sortDescriptor]; 
NSError *error ; 
NSArray *resultArray= [context executeFetchRequest:fetchRequest error:&error]; 

और डेटा जोड़ने तरह -

let entityNameObj = EntityName(context: context) 
entityNameObj.title = "title" 

या ऑब्जेक्टिव-सी में के रूप में

NSManagedObject *entityNameObj = [NSEntityDescription insertNewObjectForEntityForName:@"EntityName" inManagedObjectContext:context]; 
[entityNameObj setValue:@"someValue" forKey:@"someKey"]; 

और तरह संदर्भ बचाने -

do { 
    try self.context.save() 
} catch _ as NSError { 
    print("Error") 
} 

या में के रूप में ऑब्जेक्टिव-सी

[((AppDelegate*)[[UIApplication sharedApplication] delegate]) saveContext]; 
0

मैं एक समाधान उद्देश्य सी का उपयोग कर पाया है यह चलता है, लेकिन मुझे यकीन नहीं है कि यह सही समाधान है।

- (void)dbManager { 
NSManagedObjectContext *context = self.persistentContainer.viewContext; 
NSError *error = nil; 
if ([context hasChanges] && ![context save:&error]) { 
    // Replace this implementation with code to handle the error appropriately. 
    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
    NSLog(@"Unresolved error %@, %@", error, error.userInfo); 
    abort(); 
} 

    NSManagedObject *customAnimal = [NSEntityDescription insertNewObjectForEntityForName:@"Animals" inManagedObjectContext:context]; 
    [customAnimal setValue:@"Lion" forKey:@"type"]; 
    [customAnimal setValue:@"Rabit" forKey:@"name"]; 
    [customAnimal setValue:@"Blue" forKey:@"color"]; 
    [customAnimal setValue:@12 forKey:@"age"]; 



    NSLog(@"Get data from DB"); 

    NSMutableArray* animalsArray; 


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Animals"]; 


    animalsArray = [[context executeFetchRequest:fetchRequest error:nil] mutableCopy]; 



    NSLog(@"array is %@", animalsArray); // array is (
"<Animals: 0x6000000aee80> (entity: Animals; id: 0x60000022e120 <x-coredata:///Animals/tAAC7332D-6BEF-441C-9041-0ECB57469FA62> ; data: {\n age = 12;\n color = Blue;\n name = Rabit;\n type = Lion;\n})"  

}

3
-(void)profileDatabase 

{ NSManagedObjectContext * संदर्भ = [ADM.persistentContainer viewContext];

NSManagedObject *profile=[NSEntityDescription insertNewObjectForEntityForName:@"Profile" inManagedObjectContext:context]; 

[profile setValue:[self.serverResponseOfProfileDict objectForKey:@"firstName"] forKey:@"firstName"]; 
[profile setValue:[self.serverResponseOfProfileDict objectForKey:@"surName"] forKey:@"surName"]; 
[profile setValue:[self.serverResponseOfProfileDict objectForKey:@"batchID"] forKey:@"batchID"]; 
[profile setValue:[self.serverResponseOfProfileDict objectForKey:@"profileImagePath"] forKey:@"profileImagePath"]; 
[profile setValue:[self.serverResponseOfProfileDict objectForKey:@"registeredEmail"] forKey:@"registeredEmail"]; 
[profile setValue:[self.serverResponseOfProfileDict objectForKey:@"role"] forKey:@"role"]; 
[profile setValue:[self.serverResponseOfProfileDict objectForKey:@"studentID"] forKey:@"studentID"]; 

NSLog(@"userObj:%@",profile); 
NSError* error; 
[context save:&error]; 


NSFetchRequest *fetchRequest=[[NSFetchRequest alloc]initWithEntityName:@"Profile"]; 

fetchRequest.returnsObjectsAsFaults=NO; 
NSArray* results=[context executeFetchRequest:fetchRequest error:&error]; 
NSLog(@"Result:%@",results); 

    NSManagedObject *result=[results objectAtIndex:0]; 

    [ADM.databaseResponseOfProfileDict setObject:[result valueForKey:@"firstName"] forKey:@"firstName"]; 
    [ADM.databaseResponseOfProfileDict setObject:[result valueForKey:@"surName"] forKey:@"surName"]; 
    [ADM.databaseResponseOfProfileDict setObject:[result valueForKey:@"batchID"] forKey:@"batchID"]; 
    [ADM.databaseResponseOfProfileDict setObject:[result valueForKey:@"profileImagePath"] forKey:@"profileImagePath"]; 
    [ADM.databaseResponseOfProfileDict setObject:[result valueForKey:@"registeredEmail"] forKey:@"registeredEmail"]; 

    [ADM.databaseResponseOfProfileDict setObject:[result valueForKey:@"role"] forKey:@"role"]; 

    [ADM.databaseResponseOfProfileDict setObject:[result valueForKey:@"studentID"] forKey:@"studentID"]; 
    NSLog(@"dic:%@",ADM.databaseResponseOfProfileDict); 

}

संबंधित मुद्दे