Reputation: 53
I want to store my Rich Text for UITextView's NSAttributedString. For this as suggested in a question on stackoverflow, I choose NSData. Problem is app crashes while un-archiving and retrieved data is also not same as the saved NSData.
Explanation DB: Saving Data into DB
In DB I have a column named rtfText with datatype blob -> (rtfText blob) While Saving into DB, I do this
NSData *data = [NSKeyedArchiver archiveWithRootObject:_myTextView.attributedString];
and sends the data like this in query
NSString Query = [NSString stringWithFormat:@"Insert into table (rtfText) VALUES \"%@\")",data];
Retrieving Data From DB
Data is retrieved like this:
NSData *data = [[NSData alloc] initWithBytes:(sqlite3_column_blob(statement, 18)) length:sqlite3_column_bytes(statement, 18)];
UnArchive:
From the retrieved NSData from DB I unarchive it into NSAttributedString like this
_myTextView.attributedText = [NSKeyedUnarchiver unarchiveObjectWithData:data]; **<- App Crashes at this point giving error**
Error: -[__NSCFData objectForKey:]: unrecognized selector sent to instance 0x15d56e80
I even tried to save NSAttributedString to NSData after Archive then converting it to NSString using NSASCIIStringEncoding but DB crashes then. I also tried saving it like this. First using NSKeyedArchiver converted NSAttributedString to NSData then to NSString using NSUTF8StringEncoding which gave me null string.
Kindly look into this.
Thanks in advance
Upvotes: 1
Views: 1061
Reputation: 53
Data Never inserted properly into database. Reason is Query on sqlite3 is always in the form of UTF8. NSString has to be converted to UTF-8 to make a query. After this conversion stored NSData in database is totally changed i.e. encoded again via UTF-8.
When I retrieved the data, it was not same as the old one. Therefore got error while un archiving it.
Solution: Stored NSData into sqlite3 without converting to UTF-8 and used sqlite_bind_blob for storing blob data to sqlite3 database.
Upvotes: 1