How to store audio file in sqlite database in iOS

喜夏-厌秋 提交于 2019-12-04 20:56:19

I don't recommend to store binary files in database as blobs. it will make you a problem with growing database file size in future. You will have to implement smart vacuum operation which is very slow on large data amounts. My strong opinion: the files must be stored in file system. Store your file in application Documents folder and store a kind of link to them in database.

you can download file and write audio file using NSDocumentDirectory in app. Documents folder like

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"audio.mp3"]];
[{your audio file in NSData} writeToFile:path atomically:YES];

now store only "audio.mp3" in database

when you retrieve data from database that give you name like

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:{database store name}]];

note if you store full path that not working because Documents foldar path change whenever you run app.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!