once the application terminates, I write data on the database.

As from the new ios 4 applicationWillTerminate: is not known as, I have place the same saving code in applicationDidEnterBackground: and applicationWillTerminate:

I just read the task carried out in applicationDidEnterBackground: needs to have a couple of time.

So, when the writing around the db takes more, how could I manage this?

You receive 5 seconds to do any saving etc within the applicationDidEnterBackground: method.

If you want a lot more than 5 seconds there's this UIApplication approach to request experience thread that will permit additional time to complete anything you need:

- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void(^)(void))handler

Here's Apple's example code with this: