---
title: "execute(_:with:)"
framework: coredata
role: symbol
role_heading: Instance Method
path: "coredata/nsincrementalstore/execute(_:with:)"
---

# execute(_:with:)

Returns a value as appropriate for the given request, or nil if the request cannot be completed.

## Declaration

```swift
func execute(_ request: NSPersistentStoreRequest, with context: NSManagedObjectContext?) throws -> Any
```

## Parameters

- `request`: A fetch request.
- `context`: The managed object context used to execute request.

## Return Value

Return Value A value as appropriate for request, or nil if the request cannot be completed

## Discussion

Discussion The value to return depends on the result type (see resultType) of request: If it is NSManagedObjectResultType, NSManagedObjectIDResultType, or NSDictionaryResultType, the method should return an array containing all objects in the store matching the request. If it is NSCountResultType, the method should return an array containing an NSNumber whose value is the count of all objects in the store matching the request. If the request is a save request, the method should return an empty array. If the save request contains nil values for the inserted/updated/deleted/locked collections; you should treat it as a request to save the store metadata. You should implement this method conservatively, and expect that unknown request types may at some point be passed to the method. The correct behavior in these cases is to return nil and an error.

## See Also

### Manipulating Managed Objects

- [newValuesForObject(with:with:)](coredata/nsincrementalstore/newvaluesforobject(with:with:).md)
- [newValue(forRelationship:forObjectWith:with:)](coredata/nsincrementalstore/newvalue(forrelationship:forobjectwith:with:).md)
- [obtainPermanentIDs(for:)](coredata/nsincrementalstore/obtainpermanentids(for:).md)
- [newObjectID(for:referenceObject:)](coredata/nsincrementalstore/newobjectid(for:referenceobject:).md)
- [referenceObject(for:)](coredata/nsincrementalstore/referenceobject(for:).md)
