4

I have checked many questions available on SO such as this & this, related to these errors but let me tell you my scenario.

I am loading images on a view & after clicking assets in collection view 18 times my code returns this error. I am not doing anything complex I am just adding the same asset which is been clicked on a view above that asset.

Information: I am creating assets & saving them in documents directory & fetching them from there only.

Below is my code where error is coming:

let data = try Data(contentsOf: URL(fileURLWithPath: (contentsOfFile: (userInfo[kPath] as! String))), options: .uncached)

I am trying to get imageData & then put it as image in image view but after few clicks the Try statement is returning following errors

"Too many open files"

I have also tried another way of loading image i.e.

UIImage(contentsOfFile: imageFilePath)!

but result is same.

Can anyone help me by guiding how to solve this error?

Community
  • 1
  • 1
iYoung
  • 3,596
  • 3
  • 32
  • 59
  • Virtually every operating system has some limit on the number of files that a given process can have open at one time. The limit typically is in the range of 10,000-100,000, depending on the OS and version, but may be smaller in some cases, and database systems may impose their own limits. Especially when writing code which scans directories, it's easy to accidentally open a lot of files and not close them. – Hot Licks Jan 10 '17 at 20:22

1 Answers1

4

The code that you've shared with us is unlikely to be the source of the "too many files open" problem, but rather more likely just a symptom of another problem. Your other error, "unable to open database file" suggests a more likely culprit, e.g. you may be opening databases but not properly closing them, eventually ending up with too many files open.

I'd suggest carefully examining everywhere that you open files and double check that you're closing them properly. Especially if you're doing your own sqlite3 API calls, it's surprisingly easy to do this because files are not closed automatically. I would suggest adding logging statements everywhere you open and close files and make sure that every "open" is paired with a corresponding "close".

If the opening and closing of files is happening in Swift code where you have many if or guard statements, often putting the "close" in a defer statement is a nice way of ensuring the close is called regardless of the path of execution.


As an aside and unrelated to this file opening problem, the reference to contentsOfFile in your code snippet is misleading/unnecessary. You can simplify that to:

let data = try Data(contentsOf: URL(fileURLWithPath: userInfo[kPath] as! String), options: .uncached)
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Could you tell me, how to close those files in Swift? I am downloading png files and assigning them into UIImageViews. However, after 300-400, I am getting the same error. I am assuming closing those images after assigning them will solve this error. Hope you respond. Thank you! – Vetuka Jan 09 '18 at 22:05
  • 1
    @sc13 - As I told iYoung, i don't believe `UIImageView` would manifest this "too many files". You might run out of memory (with unexpected behavior) if you don't manage the process correctly, but `UIImage` doesn't hang on to files. (I just did quick test with 10,000 separate images in collection view, and I received no such error). I suspect you have something else going on. I'd cast your attention to other things that your app might be doing with files. If you're still having problems, post your own question with [reproducible example of the problem](https://stackoverflow.com/help/mcve). – Rob Jan 09 '18 at 23:33
  • @Rob Thank you. I already asked my [question](https://stackoverflow.com/questions/47419462/how-do-you-close-open-files-using-swift) but nobody provided an answer. – Vetuka Jan 10 '18 at 00:32