Removing Soup Elements

Traditionally, SmartStore methods let you remove soup elements by specifying an array of element IDs. To do so, you usually run a preliminary query to retrieve the candidate IDs, then call the method that performs the deletion. In Mobile SDK 4.2, SmartStore ups the game by adding a query option to its element deletion methods. With this option, you provide only a query, and SmartStore deletes all elements that satisfy that query. This approach delivers a performance boost because both the query and the deletion operation occur in a single call.

Hybrid Apps 

In hybrid apps, you use the third parameter to pass either an ID array or a SmartStore query spec.

1removeFromSoup([isGlobalStore, ]soupName, entryIdsOrQuerySpec,
2    successCB, errorCB)
3removeFromSoup([storeConfig, ]soupName, entryIdsOrQuerySpec,
4    successCB, errorCB)

In addition to success and error callbacks, this function takes the following arguments:

Parameter NameArgument Description
isGlobalStore(Optional) Boolean that indicates whether this operation occurs in a global or user-based SmartStore database. Defaults to false.
storeConfig(Optional) StoreConfig object that specifies a store name and whether the store is global or user-based.
soupNameString. Pass in the name of the soup.
entryIdsOrQuerySpecArray or QuerySpec object. Pass in the name of the soup.

Android Native Apps 

Android native methods for removing entries give you the option of either handling the transaction yourself, or letting the method handle the transaction transparently. If you set the handleTx argument to false, you’re responsible for starting the transaction before the call and ending it afterwards. If you use the overload that doesn’t include handleTx, or if you set handleTx to false, Mobile SDK handles the transaction for you.

Remove entries by ID array (Android) 

To remove entries by ID array in Android native apps, call either of the following methods:

1public void delete(String soupName, Long... soupEntryIds)
2public void delete(String soupName, Long[] soupEntryIds, boolean handleTx)

Remove entries by query (Android) 

To remove entries by query in Android native apps, call either of the following methods:

1public void deleteByQuery(String soupName, QuerySpec querySpec)
2public void deleteByQuery(String soupName, QuerySpec querySpec, boolean handleTx)

In this example, the soup “MySoup” has a string field called “key”. To delete all entries that have a key starting with “abc”, call the method like this:

1store.deleteByQuery("MySoup", QuerySpec.buildLikeQuerySpec(
2   "MySoup", "key", "abc%", "key", Order.ascending, 10));

iOS Native Apps 

Remove entries by ID array (iOS) 

To remove entries by ID array in iOS native apps, call one of these methods:

Objective-C 

1- (void)removeEntries:(NSArray*)entryIds fromSoup:(NSString*)soupName error:(NSError **)error;

Swift 

1public func remove(entryIds: [Any], forSoupName: String) -> Void

Example:

1remove(entryIds: entries, forSoupNamed: soupName)

Remove entries by query (iOS) 

To remove entries by query in iOS native apps, call one of these methods:

Objective-C 

1- (void)removeEntriesByQuery:(SFQuerySpec*)querySpec
2                    fromSoup:(NSString*)soupName;
3- (void)removeEntriesByQuery:(SFQuerySpec*)querySpec
4                    fromSoup:(NSString*)soupName
5                       error:(NSError **)error;

In this example, the soup “MySoup” has a string field called “key”. To delete all entries that have a key starting with “abc”, call the method like this:

1[store removeEntriesByQuery:[SFQuerySpec newLikeQuerySpec:
2@"MySoup"withPath:@"key" withLikeKey:@"abc%" withOrderPath:@"key" 
3withOrder:kSFSoupQuerySortOrderAscending withPageSize:10]
4fromSoup:kTestSoupName
5error:&error];

Swift 

1func remove(usingQuerySpec: QuerySpec, entryIds: [Any], forSoupNamed: String) -> Void

Example:

1var removed = removeEntries(usingQuerySpec: querySpec, forSoupNamed: soupName)

We've Moved

Welcome to the new home of the Mobile SDK Developer Guide! For now, the Japanese guide can be found in PDF form.