How i can replace object with managedObject in core data object.Please help me how i can out from this problem.
XMPPMessageArchivingCoreDataStorage *storage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"XMPPMessageArchiving_Contact_CoreDataObject"
inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKey:#"mostRecentMessageTimestamp" ascending:NO];
[request setEntity:entityDescription];
[request setSortDescriptors:#[sortDescriptor1]];
NSError *error;
NSArray *data = [moc executeFetchRequest:request error:&error];
for (XMPPMessageArchiving_Contact_CoreDataObject *managedObject in data) {
if ([managedObject.bareJid isEqual:_user.jabberId]) {
// [moc deleteObject:managedObject];**//Here i want to replace object to managed object never delete**
}
}
Related
My question is simple. How to communicate between my model and my controller ?
Code into controller
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Album" inManagedObjectContext:manageContext];
album = [[Album alloc] initWithEntity:entity insertIntoManagedObjectContext:manageContext];
NSArray *arrayListAlbums = [album returnAllAlbums];
Method into NSManagedObject
(NSArray *) returnAllAlbums
{
NSEntityDescription *entitydescription = [NSEntityDescription entityForName:#"Album" inManagedObjectContext:managedObjContextAlbum];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entitydescription];
NSError *error;
NSArray *arrayListAlbums = [managedObjContextAlbum executeFetchRequest:request error:&error];
return arrayListAlbums;
}
From the code you provided, you have added a convenience method in your NSManagedObject subclass and then you create an instance of the NSManagedObject to call that convenience method. This is not a good design.
You are creating an empty data object to call a fetch method. Perhaps you should have a class method "somewhere" that looks like this:
+ (NSArray *)returnAllAlbums:(NSManagedObjectContext*)moc
{
NSEntityDescription *entitydescription = [NSEntityDescription entityForName:#"Album" inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entitydescription];
NSError *error;
NSArray *arrayListAlbums = [managedObjContextAlbum executeFetchRequest:request error:&error];
if (arrayListAlbums == nil) {
NSLog(#"Failed to fetch: %#\n%#", [error localizedDescription], [error userInfo]);
}
return arrayListAlbums;
}
Which you can then access without creating an empty data object:
NSArray *arrayListAlbums = [MyClassWithFetchMethods returnAllAlbums:myLocalContextReference];
Warning: I don't know obj C but was handed this project for this one task
I am successfully encrypting the fields which lay in core data (sqlite) but I cannot decrypt them using a similar method. Below is my code which is causing the app to break during the fetch of core data:
NSManagedObjectContext *context = [self getContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
[request setReturnsObjectsAsFaults:NO];
if (predicate != nil) {
[request setPredicate:predicate];
}
NSError *error;
//the code that breaks
NSMutableDictionary *theFetch = [[context executeFetchRequest:request error:&error] mutableCopy];
return [[self decryptDictionaryData:theFetch withUserId:entityDesc.propertiesByName[#"theUnlockKey"]] mutableCopy];
decryptDictionaryData goes through each key in the dictionary and decrypts as follows:
decryptedResult = [RNDecryptor decryptData:[[NSData alloc] initWithBase64EncodedString:value options:0]
withSettings:kRNCryptorAES256Settings
password:key
error:&error];
I am using simple core data fetch request code.
- (NSMutableArray *) read_cd
{
NSError *error = nil;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"List_CD" inManagedObjectContext:cd_Context];
[request setEntity:entity];
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:#"last_date" ascending:NO];
NSArray *sortDescriptor = [[NSArray alloc] initWithObjects:sd, nil];
[request setSortDescriptors:sortDescriptor];
NSMutableArray *mutableFetchResults = [[cd_Context executeFetchRequest:request error:&error] mutableCopy];
if(mutableFetchResults == nil){
//Handle the error
}
return mutableFetchResults;
}
and I'm using return value like this
MSMutableArray *now_cd = self.read_cd;
cell output for {
cell.label1.text = [[now_cd objectAtIndex:i] cd_title];
cell.label2.text = [[now_cd objectAtIndex:i] cd_auth];
}
I want to set default value when executeFetchRequest is null.
so I can continue to use an existing output module.
I don't know how to make a method like that
[[object objectAtIndex:i] instance]
If I've created an NSFetchRequest on the main thread like so:
NSManagedObject *bar = ...;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Foo"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:#"SELF.bar == %#",
[bar objectID]]];
Is it safe to pass this NSFetchRequest with an NSPredicate that contains a NSManagedObjectID to a background thread like so?
NSManagedObject *bar = nil;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Foo"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:#"SELF.bar == %#",
[bar objectID]]];
NSPersistentStoreCoordinator *persistentStoreCoordinator = ...;
[[NSBlockOperation blockOperationWithBlock:^{
NSManagedObjectContext *managedObjectContext = [NSManagedObjectContext new];
[managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];
NSArray *foos = [managedObjectContext executeFetchRequest:fetchRequest
error:NULL];
}] start];
I found some example code in the CoreData release notes for iOS 5 that pretty much does this, so it looks ok.
NSFetchRequest *fr = [NSFetchRequest fetchRequestWithEntityName:#"Entity"];
__block NSUInteger rCount = 0;
[context performBlockAndWait:^() {
NSError *error;
rCount = [context countForFetchRequest:fr error:&error];
if (rCount == NSNotFound) {
// Handle the error.
}
}];
NSLog(#"Retrieved %d items", (int)rCount);
i have some Data in a TableView (loaded from coreData) and now i want to upload this data to a webserver.
Before i can do that i want to save all data to a *.txt file.
How can i load all instances of an entity / property to an mutable array?
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Event" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"barCode" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
Thanks,
brush51
Edit 1:
Can you give me a little Help (i dont want ALL data, just the property "barCode"), now i have this code:
NSManagedObjectContext *moc = [self managedObjectContext];
NSFetchRequest *req = [[NSFetchRequest alloc] init];
[req setEntity:[NSEntityDescription entityForName:#"Event" inManagedObjectContext:moc]];
[req setIncludesPropertyValues:NO];
NSError *error;
NSArray *codes = [moc executeFetchRequest:req error:&error];
[req release];
for (NSManagedObject *Event in codes) {
NSLog(#"there it is : ------------> %#", codes);
NSLog(#"barcodes ------------------> %#", [[managedObject valueForKey:#"barCode"] description]);
}
How to output ONLY the instances of the property "barCode" ??
thanks for answering,
here is my code:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Event" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"barCode" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
I have no predicate... Predicate is for what?
If you do a fetch request on the entity without a predicate, you'll get all the instances.