开发者

How do I monitor and synchronize a collection of files?

Hi I'm trying to create a simple music library for my application that uses sqlite to store information about the files. I want to know how to update my database when the files making up the library change.

  • One way I thought of figuring out whether or not I need to update the db is to count the number of files that make up the library vs. the number of files in the database. This method seems like it would take a lot of time if there were thousands of files though. Is th开发者_StackOverflow中文版ere a better way to do this?

  • How do I 'update' my db without actually going through each file again, getting file information such as artist, album, etc, and comparing it with my db to see if I need to add or remove the file from the database?


You can use several techniques.

  1. Scan all directories added by the user to see if the last modified timestamp has changed. If it has rescan the file. You can run this in a separate thread at a low priority if you like.
  2. When a user clicks on the audio track in the UI rescan the file. If it doesn't exist alert the user and remove the entry. If it does exist check the timestamp and update accordingly.
  3. Provide an option to the user to rescan the entire library.
  4. If the timestamp of a folder known to contain songs changes you can rescan it along with any sub-folders and update the library accordingly.
  5. If the file still exists you probably don't need to remove it from the database.

Checking the timestamp of the files (both created and modified) will generally give you a good idea of what updates need to be made to the database if any at all.

You can also alert the user to possible changes and asking them if they want to rescan based on the type of changes detected. Traversing subdirectories and processing a few thousand files is probably not going to take as much time as you think.

When rescanning a file you don't really need to compare it to the existing record in the database. Just execute an SQL UPDATE and be done with it. If you aren't already familiar with SQL I suggest you study and experiment with some of the example statements available around the net. Keep in mind that if you execute a single SQL transaction for each file scanned it will take considerably longer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜